Class: Edi::Commercehub::ReturnNotificationMessageProcessor

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

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

#build_return_message_from_rma(rma) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/services/edi/commercehub/return_notification_message_processor.rb', line 5

def build_return_message_from_rma(rma)
  rma_items = rma.rma_items
                 .joins(:returned_line_item)
                 .includes(:returned_line_item)
                 .where.not(line_items: { edi_line_number: nil })
                 .where(state: 'returned')
                 .order(LineItem[:edi_line_number])

  msg = Edi::Commercehub::ReturnNotificationMessage.new
  msg.ch_partner_id = orchestrator.ch_partner_id
  msg.ch_vendor_id = orchestrator.ch_vendor_id
  msg.po_number = rma.original_order.first_po_number

  msg.lines = []
  msg.return_details = []

  # Group by received date or if nil, then updated_at date on each item, will be used to create a received detail message
  rma_items.group_by { |ri| ri.received_date || ri.updated_at.to_date }.each_with_index do |(return_date, returned_items), idx|
    rd = Edi::Commercehub::ReturnNotificationMessage::ReturnDetail.new(
      id: "RD#{idx + 1}",
      rma_number: rma.reference_number,
      return_date: return_date
    )
    msg.return_details << rd

    returned_items.each do |ri|
      msg.lines << Edi::Commercehub::ReturnNotificationMessage::ReturnedLine.new(
        action: 'v_return',
        action_code: map_reason_code(ri),
        line_number: ri.returned_line_item.edi_line_number,
        trx_qty: ri.returned_item_quantity,
        return_detail: rd
      )
    end
  end
  msg
end

#map_reason_code(rma_item) ⇒ Object

This map is vendor specific technically but so far we only have one
vendor taking this message, if it differs we can move this as a hash to the
orchestrator or its own class.

changed_mind
damaged
damaged_defective
exchange
adjustment_lost_in_transit
not_as_expected
adjustment_goods_not_fulfilled
undeliverable
wrong_item



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/services/edi/commercehub/return_notification_message_processor.rb', line 95

def map_reason_code(rma_item)
  case rma_item.returned_reason
  when 'CSA'
    'arrived_too_late'
  when 'CLD'
    'changed_mind'
  when 'FLT'
    'damaged'
  when 'LIT'
    'adjustment_lost_in_transit'
  when 'SHE'
    'adjustment_goods_not_fulfilled'
  when 'TDE'
    'undeliverable'
  when 'COI'
    'wrong_item'
  else
    'changed_mind'
  end
end

#process(msg, order = nil) ⇒ Object



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

def process(msg, order = nil)
  b = Nokogiri::XML::Builder.new do |xml|
    xml.send(:ReturnMessageBatch) do
      xml.send(:partnerID, msg.ch_vendor_id)
      xml.send(:hubReturn) do
        xml.send(:participatingParty, msg.ch_partner_id, name: msg.ch_partner_name, roleType: 'merchant', participationCode: 'To:')
        xml.send(:poNumber, msg.po_number)
        msg.lines.each do |l|
          xml.send(:hubAction) do
            xml.send(:action, l.action)
            xml.send(:actionCode, l.action_code)
            xml.send(:merchantLineNumber, l.line_number)
            xml.send(:trxQty, l.trx_qty)
            xml.send(:returnDetailLink, returnDetailID: l.return_detail&.id)
          end
        end
        msg.return_details.each do |rd|
          xml.send(:returnDetail, returnDetailID: rd.id) do
            xml.send(:RMANumber, rd.rma_number)
            xml.send(:returnDate, rd.return_date.strftime('%Y%m%d'))
          end
        end
      end
      xml.send(:messageCount, msg.lines.size)
    end
  end
  EdiCommunicationLog.create_outbound_file_from_data(data: b.to_xml,
                                                     file_extension: 'return',
                                                     partner: orchestrator.partner,
                                                     data_type: 'xml',
                                                     resources: order,
                                                     category: 'return_notification')
end

#receive_rma(rma) ⇒ Object



43
44
45
46
# File 'app/services/edi/commercehub/return_notification_message_processor.rb', line 43

def receive_rma(rma)
  m = build_return_message_from_rma(rma)
  process(m, rma.original_order)
end