Class: Edi::Commercehub::PackingSlipProcessor

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

Overview

Service object: packing slip processor.

Constant Summary

Constants included from RequestIdentifiable

RequestIdentifiable::REQUEST_ID_HEADERS

Constants included from AddressAbbreviator

AddressAbbreviator::MAX_LENGTH

Instance Attribute Summary collapse

Attributes inherited from BaseEdiService

#orchestrator

Attributes inherited from BaseService

#options

Instance Method Summary collapse

Methods inherited from BaseEdiService

#amazon_feed_product_type, #duplicate_po_already_notified?, #mark_duplicate_po_as_notified, #onboard_ordered_catalog_items, #report_order_creation_issues, #safe_process_edi_communication_log

Methods included from RequestIdentifiable

#partner_request_id

Methods included from AddressAbbreviator

#abbreviate_street, #collect_street_originals, #record_address_abbreviation_notes

Methods inherited from BaseService

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

Constructor Details

#initialize(orchestrator, options = {}) ⇒ PackingSlipProcessor

Returns a new instance of PackingSlipProcessor.

Parameters:

  • orchestrator (Edi::BaseOrchestrator)

    the partner orchestrator

  • options (Hash) (defaults to: {})

    service options (passed to Edi::BaseEdiService)

Options Hash (options):

  • logger (Logger)

    logger to use (defaults to Rails.logger)



11
12
13
14
# File 'app/services/edi/commercehub/packing_slip_processor.rb', line 11

def initialize(orchestrator, options = {})
  @barcode_reader = orchestrator.packing_slip_reader
  super
end

Instance Attribute Details

#barcode_readerObject (readonly)

Returns the value of attribute barcode_reader.



6
7
8
# File 'app/services/edi/commercehub/packing_slip_processor.rb', line 6

def barcode_reader
  @barcode_reader
end

Instance Method Details

#find_order(po_number) ⇒ Object



75
76
77
78
79
80
81
# File 'app/services/edi/commercehub/packing_slip_processor.rb', line 75

def find_order(po_number)
  Order.joins(:customer, :payments)
       .merge(orchestrator.customers)
       .sales_orders.order('orders.created_at desc')
       .where(state: Order::RELEASABLE_STATES)
       .where(Payment[:po_number].eq(po_number)).first
end

#process(edi_communication_logs = nil) ⇒ Object



16
17
18
19
20
21
# File 'app/services/edi/commercehub/packing_slip_processor.rb', line 16

def process(edi_communication_logs = nil)
  edi_communication_logs ||= EdiCommunicationLog.where(state: 'ready', category: 'packing_slip', partner: orchestrator.partner)
  [edi_communication_logs].flatten.each do |ecl|
    safe_process_edi_communication_log ecl
  end
end

#process_communication_log(ecl) ⇒ Object



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

def process_communication_log(ecl)
  return unless ecl.ready? && ecl.category == 'packing_slip'

  EdiCommunicationLog.transaction do
    unless (u = ecl.uploads.first)
      # No `error!` here: the raise below rolls this transaction back, so the
      # write never lands — it only leaves the in-memory record in
      # `exception`, which used to make safe_process_edi_communication_log's
      # own error! blow up with InvalidTransition and take the rest of the
      # batch with it. Let the rescue do it, outside the transaction.
      msg = "Could not find uploads for edi communication log #{ecl.id}"
      logger.error msg
      raise msg
    end
    pdf_splitter = Utilities::PdfSplitter.new

    # Get the file locacally for some work
    local_tmp_path = Rails.root.join(Rails.application.config.x.temp_storage_path.to_s, u.attachment_name)
    u.attachment.to_file(local_tmp_path)
    # Split our pdf into multiple
    pdf_paths = pdf_splitter.process(local_tmp_path)
    errors = []
    pdf_paths.each do |pdf_path|
      po_number = barcode_reader.process(pdf_path)
      logger.info "Barcode Po Number read on file: #{po_number}"
      if po_number.present? && (order = find_order(po_number))
        logger.info "Found order #{order.reference_number}, cloning pdf"
        new_u = Upload.uploadify(pdf_path, 'custom_packing_slip_pdf', order)
        new_u.save!
        ecl.edi_documents.create(order: order, note: 'packing list') unless ecl.edi_documents.any? { |edi_doc| edi_doc.resource == order } # multiple uploads ok, only one edi document
        ecl.complete
        # We can try releasing the order
        order.release_order
      else
        msg = "Cannot match to order with po #{po_number}."
        errors << msg
        logger.error msg
      end
    end
    if errors.present?
      ecl.process_attempts ||= 0
      ecl.process_attempts += 1
      if ecl.process_attempts > 5
        ecl.notes = errors.join("\n")
        ecl.error!
      else
        ecl.save # We save the attempt, next round over will be tried again
      end
    end
  end
end