Class: Edi::Menard::InventoryMessageProcessor

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

Constant Summary collapse

VENDOR_NUMBER =
Edi::Menard::Orchestrator::VENDOR_NUMBER
HEADER_ROW =
2
DATA_START_ROW =
3
DEFAULT_RESTOCK_LEAD_TIME =
3.months
HEADERS =
[
  'Sku#',
  'Menard Inc. Sales Unit',
  'Model#',
  'Item Description',
  'Qty on Hand',
  'Date when back in stock (YYYY/MM/DD)',
  'QTY to be available when back in stock',
  'Status',
  'Discontinued Date (YYYY/MM/DD)'
].freeze

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

#generate_preview_workbook(catalog_items) ⇒ Object



69
70
71
# File 'app/services/edi/menard/inventory_message_processor.rb', line 69

def generate_preview_workbook(catalog_items)
  build_workbook(catalog_items)
end

#load_catalog_itemsObject



60
61
62
63
64
65
66
67
# File 'app/services/edi/menard/inventory_message_processor.rb', line 60

def load_catalog_items
  base = orchestrator.customer.catalog.catalog_items
  base
    .where(state: %w[active pending_discontinue])
    .joins(store_item: :item)
    .includes(store_item: :item)
    .order('items.sku')
end

#process(catalog_items: nil, triggered_by: 'scheduled') ⇒ Object



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

def process(catalog_items: nil, triggered_by: 'scheduled')
  logger.info "Creating inventory feed for partner #{orchestrator.partner}"
  catalog_items ||= load_catalog_items
  return :empty_set unless catalog_items.present?

  catalog_items = catalog_items.to_a
  file_path = generate_spreadsheet(catalog_items)

  ecl = nil
  EdiCommunicationLog.transaction do
    ecl = EdiCommunicationLog.create!(
      partner: orchestrator.partner.to_s,
      state: 'ready',
      file_name: File.basename(file_path),
      category: 'inventory_advice',
      data: build_summary_csv(catalog_items),
      data_type: 'csv',
      file_info: {
        triggered_by: triggered_by,
        item_count: catalog_items.size,
        vendor_number: VENDOR_NUMBER,
        catalog_id: orchestrator.catalog_id
      }
    )
    catalog_items.each do |ci|
      ecl.edi_documents.create!(resource: ci)
    end

    attach_spreadsheet(ecl, file_path)
  end

  cleanup_file(file_path)
  ecl
end