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 Attribute Summary

Attributes inherited from BaseService

#options

Instance Method Summary collapse

Methods inherited from BaseService

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

Constructor Details

This class inherits a constructor from BaseService

Instance Method Details

#archive(item) ⇒ Object



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

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').find_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
# File 'app/services/item/archive_item.rb', line 5

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