Class: Edi::Commercehub::PackingSlipProcessor

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

Constant Summary

Constants included from AddressAbbreviator

AddressAbbreviator::MAX_LENGTH

Instance Attribute Summary collapse

Attributes inherited from BaseEdiService

#orchestrator

Instance Method Summary collapse

Methods inherited from BaseEdiService

#duplicate_po_already_notified?, #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

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

Constructor Details

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

Returns a new instance of PackingSlipProcessor.



8
9
10
11
# File 'app/services/edi/commercehub/packing_slip_processor.rb', line 8

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



69
70
71
72
73
74
75
# File 'app/services/edi/commercehub/packing_slip_processor.rb', line 69

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



13
14
15
16
17
18
# File 'app/services/edi/commercehub/packing_slip_processor.rb', line 13

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



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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/services/edi/commercehub/packing_slip_processor.rb', line 20

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

  EdiCommunicationLog.transaction do
    unless u = ecl.uploads.first
      ecl.notes = 'Upload missing!'
      ecl.error!
      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 transaction
end