Class: Edi::Commercehub::OrderMessageProcessor

Inherits:
BaseEdiService show all
Defined in:
app/services/edi/commercehub/order_message_processor.rb

Defined Under Namespace

Classes: BatchProcessResult, LineCreationResult, Result

Constant Summary

Constants included from AddressAbbreviator

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 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

#apply_payment(order, order_xml) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'app/services/edi/commercehub/order_message_processor.rb', line 201

def apply_payment(order, order_xml)
  order.reload
  order.pending_payment!
  total_due = order.total
  order.payments.build.tap do |payment|
    payment.category = Payment::PO
    payment.currency = order.currency
    payment.amount = total_due
    payment.state = 'authorized'
    payment.po_number = order_xml.xpath('poNumber').text # 00000127535003
    payment.delivery = order.deliveries.quoting.first
    payment.save!
  end
end

#apply_signature_override(order, line_total) ⇒ Object



236
237
238
239
240
241
242
243
244
245
# File 'app/services/edi/commercehub/order_message_processor.rb', line 236

def apply_signature_override(order, line_total)
  return unless order.customer&.is_part_of_lowes_ca? && line_total.to_f > 250.0 && order.signature_confirmation != true

  # apply this from Sean O'Donnell | Lowe's.ca Associate | BOT/Store Pod, seaodon@rona.ca: Please make note,orders over $250 shipped with Purolator need to have a signature required.
  shipping_code_with_signature = orchestrator.ship_code_mapper.hw_to_ch(order.shipping_method, signature: true)
  shipping_option_result = match_shipping_code(shipping_code_with_signature)
  order.edi_original_ship_code = shipping_code_with_signature
  order.signature_confirmation = true
  order.shipping_method = shipping_option_result.name
end

#build_freight_collect_account(customer:, shipping_option_name:, freight_account:) ⇒ Object

This method builds a freight collect account in the customer if it doesn't already exists
Returns nil if shipping option or freight account are not specified
Returns the instance of ShippingAccountNumber created
Raises an exception if the shipping option name is specified but not found or if the
new shipping account cannot be created



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'app/services/edi/commercehub/order_message_processor.rb', line 168

def (customer:, shipping_option_name:, freight_account:)
  return unless shipping_option_name && 
  # Do we already have this freight collect account in the customer?
  return if customer..where(account_number: ).exists?

  # Find the carrier
  shipping_option = ShippingOption.active.where(name: shipping_option_name).first || ShippingOption.where(name: shipping_option_name).first
  raise "Cannot find shipping option #{shipping_option_name}" unless shipping_option

  carrier = shipping_option.carrier
  country_iso3 = shipping_option.country_iso3
  # Find the billing zip code, we have to hunt a country native postal code, note that we assume a lot but there's no other way
  address = [customer.billing_address, customer.shipping_address, *customer.addresses].compact.detect { |a| a.country_iso3 == country_iso3 }
  zip = address.zip
  # Create the option
  customer..create!(
    carrier: carrier,
    account_number: ,
    billing_zip: zip.gsub(/\s+/, ''),
    country_iso3: country_iso3
  )
end

#create_line_items(order, order_xml) ⇒ Object



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
# File 'app/services/edi/commercehub/order_message_processor.rb', line 247

def create_line_items(order, order_xml)
  line_total = BigDecimal(0)
  cust = order.customer
  all_skus_valid = true
  errors = []
  bad_vendor_skus = []
  bad_merchant_skus = []
  # Look for an item that is in a state suitable to be accepted by the merchant partner
  catalog_items = cust.catalog.catalog_items.where(state: %w[active active_hidden require_vendor_update pending_vendor_update pending_discontinue]).order('state ASC')
  order_xml.xpath('lineItem').each do |line_xml|
    merchant_sku = line_xml.xpath('merchantSKU')&.text
    vendor_sku = line_xml.xpath('vendorSKU')&.text
    shopping_cart_sku = line_xml.xpath('shoppingCartSKU')&.text
    upc = line_xml.xpath('UPC')&.text
    raise 'Missing identifier sku in file' if vendor_sku.blank? || merchant_sku.blank?

    # Typical HD transmission:
    # <UPC>881308011079</UPC>
    # <description>Infinity 10-Bar Electric Towel Warmer in Brushed Stainless Steel</description>
    # <merchantSKU>1000726842</merchantSKU>
    # <vendorSKU>TW-F10BS-HW</vendorSKU>
    # <shoppingCartSKU>203917550</shoppingCartSKU>
    # First we try by promo sku
    matched_catalog_item = catalog_items.find_by(third_party_promo_part_number: merchant_sku) if merchant_sku.present?
    # If no match on that, try by merchant sku
    matched_catalog_item ||= catalog_items.find_by(third_party_part_number: merchant_sku) if merchant_sku.present?
    # Try shopping_cart_sku
    matched_catalog_item ||= catalog_items.find_by(third_party_part_number: shopping_cart_sku) if shopping_cart_sku.present?
    if vendor_sku.present?
      # vendor sku vs third_party_part_number
      matched_catalog_item ||= catalog_items.find_by(third_party_part_number: vendor_sku)
      # If no match, try third party sku but only on active items
      matched_catalog_item ||= catalog_items.find_by(third_party_sku: vendor_sku)
      # If no match, use our vendor SKU
      matched_catalog_item ||= catalog_items.by_skus(vendor_sku).first
    end
    # try UPC
    matched_catalog_item ||= catalog_items.by_upcs(upc).first if upc.present?
    # And finally, try merchant SKU
    matched_catalog_item ||= catalog_items.by_skus(merchant_sku).first if merchant_sku.present?

    # If we're in dev mode, they're sending us garbage anyway, just pick any old item, but let's pick a small one
    matched_catalog_item ||= catalog_items.active.in_stock.joins(:item).merge(Item.goods).order('items.shipping_weight').first if orchestrator.test_mode? && merchant_sku != '222222222'

    if matched_catalog_item
      order.line_items.build.tap do |line_item|
        line_xml.xpath('unitOfMeasure').text # EA
        # Frankly we don't care the uom, it is always one of whatever we have listed with them
        # raise "Unknown unit of measure in edi order id #{order.customer_reference}" unless %w[EA PK].include?(uom) # for now we can't handle anything else

        line_item.quantity = line_xml.xpath('qtyOrdered').text.to_i # 3
        line_item.edi_reference = line_xml.xpath('lineItemId').text # 120101514
        line_item.edi_line_number = line_xml.xpath('merchantLineNumber').text # 1

        line_xml.xpath('merchDept')&.text # Costco
        line_xml.xpath('expectedShipDate')&.text # Costco
        line_xml.xpath('requestedArrivalDate')&.text # Costco
        line_xml.xpath('poLineData/vendorDescription')&.text # Costco
        serial_number_required = line_xml.xpath('lineitemSupplemental/productFlags/@serializedProduct')&.text&.to_b # Costco

        line_item.catalog_item = matched_catalog_item
        price = BigDecimal(line_xml.xpath('unitCost').text)
        line_item.edi_unit_cost = price
        line_total += price * line_item.quantity

        raise "Line item requires serial number for EDI, but line item is not set up for serial numbers! Line item SKU is #{matched_catalog_item.item&.sku}." if serial_number_required && !matched_catalog_item.item&.require_reservation?
        # line_xml.xpath('orderLineNumber').text # 1
        # line_xml.xpath('description').text
        # line_xml.xpath('description2').text
        # line_xml.xpath('merchantSKU').text
        # line_xml.xpath('shippingCode').text
        # line_xml.xpath('requestedArrivalDate').text
        # line_xml.xpath('poLineData/unitShippingWeight').text #weight
        # line_xml.xpath('poLineData/unitShippingWeight/@weightUnit').text #weight unit LB
      end
      if matched_catalog_item.state_requires_edi_warning_on_order?
        subj = "EDI order for orchestrator: #{orchestrator.partner}, processed, but catalog item with SKU #{matched_catalog_item.sku} in state #{matched_catalog_item.state} (i.e. not active state)"
        msg = "EDI order for orchestrator: #{orchestrator.partner}, catalog item with SKU #{matched_catalog_item.sku} in state #{matched_catalog_item.state} (i.e. not active state). The order #{order.reference_number} has been processed but this is an indication of a possible issue with this catalog item."
        InternalMailer.notify_edi_admin_of_warning(subj, msg).deliver_later
      end
    else
      all_skus_valid = false
      bad_vendor_skus << vendor_sku.presence
      bad_merchant_skus << merchant_sku.presence
      errors << "Unknown vendor sku: #{vendor_sku} / merchant sku: #{merchant_sku}"
    end
  end
  order.recalculate_shipping = true
  order.save!
  LineCreationResult.new(line_total: line_total, lines_created: order.line_items.size, all_skus_valid: all_skus_valid, error_message: errors.to_sentence.capitalize, bad_vendor_skus: bad_vendor_skus.compact,
bad_merchant_skus: bad_merchant_skus.compact)
end

#customer(segment = nil) ⇒ Object



15
16
17
# File 'app/services/edi/commercehub/order_message_processor.rb', line 15

def customer(segment = nil)
  orchestrator.customer(segment)
end

#extract_ship_to_info(order_xml) ⇒ Object



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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'app/services/edi/commercehub/order_message_processor.rb', line 340

def extract_ship_to_info(order_xml)
  res = {}
  ship_to_person_place_id = order_xml.xpath('shipTo/@personPlaceID').text # <shipTo personPlaceID="PP3006376894"/>
  ship_to_xml = order_xml.xpath("personPlace[@personPlaceID='#{ship_to_person_place_id}']")

  raw_street1 = ship_to_xml.xpath('address1').text
  raw_street2 = ship_to_xml.xpath('address2').text
  originals = collect_street_originals(street1: raw_street1, street2: raw_street2)

  address = Address.new
  address.person_name_override = ship_to_xml.xpath('name1').text
  address.company_name_override = ship_to_xml.xpath('personPlaceData/companyName').text
  address.street1 = abbreviate_street(raw_street1)
  address.street2 = abbreviate_street(raw_street2)
  address.city = ship_to_xml.xpath('city').text
  address.state_code = ship_to_xml.xpath('state').text
  address.zip = ship_to_xml.xpath('postalCode').text.to_s.upcase.delete(' ').strip
  # address.country_iso3 = 'CAN' # Costco Canada, see if state code can pull it off

  # handle residential/commercial flag if present, otherwise let it default
  # <xs:element name="addressRateClass" minOccurs="0">
  #   <xs:annotation>
  #     <xs:documentation>"1" = residential,  "2" = commercial</xs:documentation>
  #   </xs:annotation>
  # </xs:element>
  if ship_to_xml.xpath('addressRateClass').text == '1' # Residential
    address.is_residential = true
  elsif ship_to_xml.xpath('addressRateClass').text == '2' # Commercial
    address.is_residential = false
  end
  address.disable_address_correction = true
  address.override_all_address_validation = true
  address.save!
  res[:address] = address
  res[:address_originals] = originals
  res[:email] = ship_to_xml.xpath('email')&.text
  res[:phone] = ship_to_xml.xpath('dayPhone')&.text
  res
end

#match_shipping_code(ch_shipping_code) ⇒ Object

Returns the shipping option name and special service to use like saturday delivery
Pass in a commercehub shipping code



382
383
384
385
386
# File 'app/services/edi/commercehub/order_message_processor.rb', line 382

def match_shipping_code(ch_shipping_code)
  raise "Unmatched shipping code #{ch_shipping_code}" unless match = orchestrator.ship_code_mapper.ch_to_hw(ch_shipping_code)

  match
end

#process(edi_logs = nil, edi_transaction_id: nil) ⇒ Object

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



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
# File 'app/services/edi/commercehub/order_message_processor.rb', line 20

def process(edi_logs = nil, edi_transaction_id: nil)
  edi_logs ||= EdiCommunicationLog.requiring_processing.where(partner: orchestrator.partner, category: 'order_batch')
  edi_logs = [edi_logs].flatten # This way you can pass a single edi communication log
  # Wrap in one big transaction
  batch_process_results = []
  edi_logs.each do |edi_log|
    log_info "Starting processing edi communication log #{edi_log.id}"
    ErrorReporting.scoped({ edi_log_id: edi_log.id }) do
      EdiCommunicationLog.transaction do
        # Process the orders in the batch
        batch_process_result = process_orders(edi_log.data, edi_transaction_id: edi_transaction_id, edi_log_id: edi_log.id)
        # Record some meta data in file info
        edi_log.file_info ||= {}
        edi_log.file_info[:orders_in_batch] = batch_process_result.orders_in_batch
        # Mark our edi log as processed, we need some error handling at one point
        # Associate the created orders
        batch_process_result.orders_created.each do |order|
          edi_log.edi_documents.create(order: order)
        end
        # Generate an acknowledgement
        orchestrator.fa_message_processor.process(batch_process_result)
        batch_process_results << batch_process_result
        edi_log.complete!
      end # End transaction
    rescue StandardError => e
      edi_log.notes = "#{e} at #{e&.backtrace_locations&.first} for edi log #{edi_log.id}"
      edi_log.error
      ErrorReporting.error(e, edi_communication_log_id: edi_log.id)
      # End transaction
    end
  end # End edi log iteration
  report_order_creation_issues(batch_process_results)
  Result.new(batch_process_results: batch_process_results)
end

#process_order(order_xml = nil) ⇒ Object

Process a single order



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
# File 'app/services/edi/commercehub/order_message_processor.rb', line 88

def process_order(order_xml = nil)
  segment = order_xml.xpath('salesDivision').text
  cust = customer(segment)
  order = cust.orders.build
  order.edi_original_order_message = order_xml.to_s
  order.edi_orchestrator_partner = orchestrator.partner.to_s
  # Not sure we need any of those?
  order.edi_order_date = Date.parse(order_xml.xpath('orderDate').text) # Parse "20180409"
  Date.parse(order_xml.xpath('custOrderDate').text) if order_xml.xpath('custOrderDate').text.present? # Costco
  order_xml.xpath('controlNumber').text.presence.to_i

  # order.order_reception_type = 'EDI' ?
  # order.shipping_phone = ?
  order.edi_transaction_id = order_xml.xpath('@transactionID').text
  order.edi_po_number = order_xml.xpath('poNumber').text
  ship_to_info = extract_ship_to_info(order_xml)
  order.shipping_address = ship_to_info[:address]
  address_originals = ship_to_info[:address_originals]
  order.tracking_email << ship_to_info[:email] if ship_to_info[:email].present?
  order.shipping_phone = ship_to_info[:phone]
  recipient_name = order.shipping_address.person_name_override || order.shipping_address.company_name_override
  commercehub_order_id = order_xml.xpath('orderId').text
  order.customer_reference = "Partner Order Nbr: #{order_xml.xpath('custOrderNumber').text} | Commercehub Order Id: #{commercehub_order_id}"
  # original currency
  order.currency = order_xml.xpath('poHdrData/offerCurrency').text.presence
  # If not use default
  order.currency ||= cust.store.currency
  order_xml.xpath('poHdrData/packslipTemplate').text.presence # Costco
  order.attention_name_override = recipient_name
  order.edi_shipping_option_name = set_shipping_option(order, order_xml)
  (customer: cust,
                                shipping_option_name: order.edi_shipping_option_name,
                                freight_account: order_xml.xpath('poHdrData/freightCollectAccount').text.presence)
  order.single_origin = true
  order.edi_is_pick_slip_required = cust.customer_record&.always_custom_packing_list?
  # Prevent auto coupons on EDI imports with externally set pricing/discounts
  order.disable_auto_coupon = true
  order.save!
  record_address_abbreviation_notes(order, address_originals, order.shipping_address)
  line_creation_result = create_line_items(order, order_xml)
  raise line_creation_result.error_message unless line_creation_result.all_skus_valid

  apply_signature_override(order, line_creation_result.line_total)

  apply_payment(order, order_xml)

  # Revenue before taxes (for price comparison), if price differs, problem, cr hold
  if orchestrator.test_mode?
    order.price_match = order.line_total
  else
    order.line_total
    line_creation_result.line_total
    order.price_match = line_creation_result.line_total # will get save when we call the event
  end
  order.state = 'in_cr_hold' # Set this as the default state
  order.save!
  order.reset_discount # temporary measure to force sanity and have revenue show on the order
  order.save!
  if (same_po_orders = Order.where(edi_po_number: order.edi_po_number).where.not(id: order.id)).any?
    # warn but don't release orders for every duplicate PO, let someone do it manually
    subj = "EDI order message for orchestrator: #{orchestrator.partner}, order #{order.reference_number} CREATED, order message PO: #{order.edi_po_number}, duplicate"
    msg = "EDI order for orchestrator: #{orchestrator.partner}, order #{order.reference_number}, has the same PO #{order.edi_po_number} as the following older EDI orders: #{same_po_orders.map do |o|
      "#{o.reference_number} (created #{o.created_at.to_date})"
    end.join(', ')}. It has been processed but this is an indication of a possible duplicate order or other issue."
    InternalMailer.notify_edi_admin_of_warning(subj, msg).deliver_later
  else
    order.reload.release_order # Then try to release, if there's any issue (discrepancy, etc) the order will remain on hold
  end

  # simplest thing to do is to flag the issue and raise an exception, don't even create the order

  # end
  order
end

#process_orders(orders_data, edi_transaction_id: nil, edi_log_id: nil) ⇒ Object

Iterates through the batch order data



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
# File 'app/services/edi/commercehub/order_message_processor.rb', line 56

def process_orders(orders_data, edi_transaction_id: nil, edi_log_id: nil)
  xml_doc = Nokogiri::XML(orders_data)
  orders_created = []
  batch_number = xml_doc.xpath('//OrderMessageBatch/@batchNumber').text
  orders_in_batch = 0
  xml_doc.xpath('//hubOrder').each do |order_xml|
    # If a specific edi_transaction_id is to be processed filter it here
    next if edi_transaction_id && order_xml.xpath('@transactionID').text != edi_transaction_id

    log_info "Processing order #{order_xml.inspect}"
    orders_in_batch += 1
    po_number = order_xml.xpath('poNumber').text
    if Order.where('created_at > ?', 12.months.ago).where(edi_po_number: po_number).count.zero? # only process these once, allow for duplicate EDI PO numbers (it has happened), as long as it has been at least 12 months old. We want to eliminate duplicate orders with the same PO, from message to message i.e. within ~the same day
      orders_created << process_order(order_xml)
    elsif (same_po_orders = Order.where('created_at < ?', 4.days.ago).where('created_at > ?', 12.months.ago).where(edi_po_number: po_number)).any? # otherwise warn that we skipped creating order because we found a matching PO order created older than four days ago, which might indicate the small chance of an order PO collision from another EDI partner
      # Only send notification if we haven't already notified about this duplicate PO
      unless duplicate_po_already_notified?(po_number, edi_log_id)
        subj = "EDI order message for orchestrator: #{orchestrator.partner}, SKIPPED, order message PO: #{po_number}, duplicate"
        msg = "EDI order message for orchestrator: #{orchestrator.partner}, EDI Communication log ID: #{edi_log_id}, order message PO: #{po_number}, has the same PO as the following recent EDI orders: #{same_po_orders.map do |o|
          "#{o.reference_number} (created #{o.created_at.to_date})"
        end.join(', ')}. It has been NOT been processed but SKIPPED as this is an indication of a duplicate order. Please ensure there is no PO number from another partner that might inadvertently match."
        InternalMailer.notify_edi_admin_of_warning(subj, msg).deliver_later
        mark_duplicate_po_as_notified(po_number, edi_log_id)
      end
    end
  end
  BatchProcessResult.new(batch_number: batch_number,
                         orders_created: orders_created,
                         orders_in_batch: orders_in_batch)
end

#set_shipping_option(order, order_xml) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'app/services/edi/commercehub/order_message_processor.rb', line 216

def set_shipping_option(order, order_xml)
  shipping_code = order_xml.xpath('shippingCode').text # UNSP
  shipping_option_result = match_shipping_code(shipping_code)
  order.edi_original_ship_code = shipping_code
  order.signature_confirmation = shipping_option_result.signature
  order.shipping_method = shipping_option_result.name
  so = ShippingOption.find_by(name: order.shipping_method)
  reqShipDate = order_xml.xpath('reqShipDate')&.text
  reqShipDate ||= order_xml.xpath('lineItem')&.first&.xpath('expectedShipDate')&.text # Lowe's
  order.requested_ship_on_or_after = Date.parse(reqShipDate) if reqShipDate.present?
  requestedArrivalDate = order_xml.xpath('lineItem')&.first&.xpath('requestedArrivalDate')&.text
  requestedArrivalDate ||= order_xml.xpath('lineItem')&.first&.xpath('poLineData')&.first&.xpath('lineReqDelvDate')&.text # Lowe's
  order.requested_ship_before = (Date.parse(requestedArrivalDate) - so.days_commitment.floor.days + 1.day) if requestedArrivalDate.present? # estimate the last date we can ship this (optimistically)
  if order.requested_ship_on_or_after && (order.requested_ship_on_or_after > Date.current)
    order.do_not_reserve_stock = false # we want to reserve the stock
    order.future_release_date = order.requested_ship_on_or_after
  end
  order.shipping_method
end

#shipping_invalid?(order) ⇒ Boolean

Returns:

  • (Boolean)


191
192
193
194
195
196
197
198
199
# File 'app/services/edi/commercehub/order_message_processor.rb', line 191

def shipping_invalid?(order)
  dq = order.deliveries.quoting.first
  return true unless dq
  return true if dq.shipping_option.is_override? || dq.shipping_option.is_third_party_only?
  return true unless dq.shipping_option.name == order.edi_shipping_option_name
  return true unless dq.signature_confirmation == order.signature_confirmation

  false
end