Class: Catalog::SynchronizeCatalogPrices

Inherits:
BaseService show all
Defined in:
app/services/catalog/synchronize_catalog_prices.rb

Overview

Operation to automatically process time synched catalog's price propagation rules

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Methods inherited from BaseService

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

Constructor Details

#initialize(options = {}) ⇒ SynchronizeCatalogPrices

Returns a new instance of SynchronizeCatalogPrices.



9
10
11
12
# File 'app/services/catalog/synchronize_catalog_prices.rb', line 9

def initialize(options = {})
  @push_catalog_item_price_service = options[:push_catalog_item_price_service] || Catalog::PushCatalogItemPrice.new
  super
end

Instance Method Details

#process(catalogs_to_price_sync = nil, options = {}) ⇒ Object



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
# File 'app/services/catalog/synchronize_catalog_prices.rb', line 14

def process(catalogs_to_price_sync = nil, options = {})
  if catalogs_to_price_sync.is_a?(Catalog)
    catalogs_to_price_sync = [catalogs_to_price_sync]
  else
    catalogs_to_price_sync ||= Catalog.active.price_sync_timed
    catalogs_to_price_sync = catalogs_to_price_sync
                             .joins(:parent_catalog)
                             .includes(:parent_catalog)
  end
  messages = []
  catalog_items_updated = []
  catalog_items_failed = []
  catalogs_to_price_sync.each do |child_catalog|
    unless child_catalog.parent_catalog_discount
      msg = " Catalog id : #{child_catalog.id} does not have a valid parent catalog discount, price sync is not applicable"
      messages << msg
      logger.error msg
      next
    end
    target_catalog_items = child_catalog.catalog_items
    target_catalog_items = block_given? ? yield(target_catalog_items) : target_catalog_items.not_discontinued
    target_catalog_items.each do |child_catalog_item|
      logger.info "Considering catalog item #{child_catalog_item.id} for update based on parent"
      unless options[:force_sync] || (child_catalog.price_sync_timed? && child_catalog_item.price_out_of_date?)
        logger.info ' -- catalog item price still considered current according to pricing delay rules'
        next
      end
      old_amount = child_catalog_item.amount
      parent_catalog_item = child_catalog_item.parent_catalog_item
      if parent_catalog_item
        new_amount = (parent_catalog_item.amount * (1.0 - child_catalog.parent_catalog_discount)).round(2)
        if new_amount == child_catalog_item.amount
          logger.info "Child catalog item id #{child_catalog_item.id} does not need a price change"
        elsif child_catalog_item.update(amount: new_amount)
          messages << msg = " -- #{old_amount} -> #{new_amount}"
          logger.info msg
          catalog_items_updated << child_catalog_item
        else
          child_catalog_item.errors.full_messages.each { |m| logger.error m }
          catalog_items_failed << child_catalog_item
          messages += child_catalog_item.errors.full_messages
        end
      else
        msg = "Child catalog item id #{child_catalog_item.id} does not have a discoverable parent"
        logger.error msg
        catalog_items_failed << child_catalog_item
        messages << msg
      end
    end
  end
  Result.new(all_items_synchronized: catalog_items_failed.empty?,
             catalogs: catalogs_with_price_sync,
             catalog_items_updated:,
             catalog_items_failed:,
             messages:)
end