Class: Item::Cloner

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

Defined Under Namespace

Classes: Result

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

#process(original, action_type, catalog_ids) ⇒ Object



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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/services/item/cloner.rb', line 7

def process(original, action_type, catalog_ids)
  #We clone the item
  item = original.deep_dup

  case action_type
  when 'clone'
    item.sku = item.new_revision_sku
  when 'refurbish'
    item.sku = item.generate_refurbished_sku
    item.name = item.name + " - Refurbished"
    item.new_item_id = original.id
    item.condition = 'refurbished'
    item.do_not_replenish = true
    item.qty_warn_on_stock = 0
    item.legacy_qty_out_of_stock = 0
    item.require_reservation = false
    item.skip_serial_number_reservation_screen = true
    item.do_not_replenish = true
    item.moq = 1
    item.upc_on_box = false
    item.visible_for_support = false
    item.review_product_weight_flag = false
    item.review_product_packaging_flag = false
    item.last_sale_date = nil
    item.first_sale_date = nil
    item.popularity_offset = 0
    item.popularity = 0
    item.always_available_online = false
    item.dropship = false
    if original.upc.present?
      item.upc = Item::UpcMaker.new.process
    end
  end

  #doing this so it doesn't throw an error during check_serial_number_presence validation
  item.require_reservation = false

  if item.save
    #We're going to mark the old item discontinued ONLY if there's no stock
    if action_type == 'clone' and item.product_category and item.product_category.is_publication?
      if original.has_stock?
        original.is_discontinued = false
        result_notice = "has stock and was NOT discontinued but it was marked as not unavailable to public."
      else
        original.is_discontinued = true
        result_notice = "was marked discontinued and not available to public."
      end
      if original.save
        res_message = "#{original.sku} #{result_notice}"
      else
        res_message = "#{original.sku} could not be updated due to errors: #{errors_to_s}"
      end
    elsif action_type == 'refurbish'
      #item.generate_store_items
      item.save
      # The cogs from the original store items will be copied over
      # This is what Venu wants
      item.store_items.each do |si|
        next unless unit_cogs = original.store_items.find_by(store_id: si.store_id)&.unit_cogs
        si.update_attribute(:unit_cogs, unit_cogs)
      end

      Item::ProductSpecificationsCloner.new.process(item)
      if catalog_ids.present?
        catalog_ids.each do |catalog_id|
          catalog = Catalog.find(catalog_id)
          store_item = item.store_items.where(store_id: catalog.store_id).first
          original_ci = original.catalog_items.find_by(catalog_id: catalog_id)
          amount = original_ci.amount - (original_ci.amount * ItemConstants::REFURB_DISCOUNT) rescue nil
          default_state = original_ci.state
          result = Catalog::CreateCatalogItem.new.process(catalog, { store_item_id: store_item.id, amount: amount, state: default_state }) if catalog.present? and store_item.present?
          if result.present? and result.catalog_item_created?
            res_message= result.messages.join(', ')
          else
            res_message = "The catalog item for catalog #{catalog.id} couldn't be created."
          end
        end
      end
    else
      res_message = "#{original.sku} was cloned successfully"
    end

    return Result.new(success: true, item: item, result_message: res_message)

  else
    res_message = "Could not clone #{original.sku}, errors: #{item.errors_to_s}"

    return Result.new(success: false, item: item, result_message: res_message)
  end

end