Class: Edi::AmazonVc::InventoryMessageProcessor

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

Constant Summary

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

#build_json(catalog_items: nil, states: nil) ⇒ Object



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

def build_json(catalog_items: nil, states: nil)
  logger.info "#{catalog_items.size} items in inventory payload"
  items = catalog_items.map do |ci|
    discontinued = ci.discontinued? || ci.pending_discontinue? || ci.in_hide_from_feed_state?
    if discontinued
      qty = 0
      discontinued = true
    else
      qty = ci.reported_stock(use_alternate_warehouse: false)
      discontinued = false
    end
    {
      buyerProductIdentifier: ci.amazon_asin,
      vendorProductIdentifier: ci.reported_vendor_sku,
      availableQuantity: {
        amount: qty,
        unitOfMeasure: 'Each'
      },
      isObsolete: discontinued
    }
  end

  {
    inventory:
      {
        sellingParty: {
          partyId: orchestrator.vendor_code
        },
        isFullUpdate: true,
        items:
      }

  }.to_json
end

#load_catalog_items(states: nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/services/edi/amazon_vc/inventory_message_processor.rb', line 28

def load_catalog_items(states: nil)
  states ||= %w[active require_vendor_update pending_vendor_update pending_discontinue]
  catalog_item_ids = []
  orchestrator.customers.each do |customer|
    catalog_items = customer.catalog.catalog_items.where(state: states).amazons_with_asins
    catalog_item_ids += catalog_items.pluck(:id)
  end
  CatalogItem.where(id: catalog_item_ids.uniq)
             .with_item
             .eager_load(:store_item, :item)
             .order(Item[:sku])
end

#process(catalog_items: nil, states: nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/services/edi/amazon_vc/inventory_message_processor.rb', line 5

def process(catalog_items: nil, states: nil)
  ecl = nil
  EdiCommunicationLog.transaction do
    logger.info "Creating inventory advice for partner #{orchestrator.partner}"
    catalog_items ||= load_catalog_items(states:)
    return :empty_set unless catalog_items.present?

    catalog_items.in_batches(of: 1000).each_with_index do |catalog_items_batch, _batch_index|
      data = build_json(catalog_items: catalog_items_batch, states:)
      ecl = EdiCommunicationLog.create_outbound_file_from_data(
        data:,
        file_extension: 'inv',
        partner: orchestrator.partner,
        category: 'inventory_advice',
        resources: catalog_items,
        file_info: {},
        data_type: 'json'
      )
    end
  end
  ecl
end