Class: Item::ArchiveItem

Inherits:
BaseService show all
Defined in:
app/services/item/archive_item.rb

Overview

This service can help you discontinue items by taking care of moving
the stock to scrap and discontinuing all catalog items and store items automatically

Instance Method Summary collapse

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 BaseService

Instance Method Details

#archive(item) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/services/item/archive_item.rb', line 18

def archive(item)
  puts "Evaluating item #{item.sku}"
  Item.transaction do
    # First move stock to scrap
    item.store_items.where(StoreItem[:qty_on_hand].gt(0)).where.not(location: 'SCRAP').each do |store_item|
      transfer_args = { '1': {
        store_item_id: store_item.id,
        new_location: 'SCRAP',
        qty: store_item.qty_on_hand
      }}
      store = store_item.store
      ItemLedgerEntry.location_transfer(store, transfer_args, Date.current, 'Archive obsolete item')
    end
    # Discontinue all
    item.discontinue_self_and_dependents
  end
end

#process(item_ids) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'app/services/item/archive_item.rb', line 5

def process(item_ids)
  res = []
  Item.where(id: item_ids).where(is_discontinued: false).each do |item|
    begin
      archive(item)
      res << { item_id: item.id, item_sku: item.sku, success: true }
    rescue StandardError => exc
      res << { item_id: item.id, item_sku: item.sku, success: false, error: exc.to_s}
    end
  end
  res
end