Class: Catalog::PushCatalogItemPrice

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

Overview

Operation to push and propage a catalog price change to the children based on
price rules

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(parent_catalog_item, options = {}) ⇒ Object

Processes a catalog item and push price updates to all its children
A price out of date check is automatic according to catalog sync policy
However passing the option ignore_out_of_date_check: true will force
the update to proceed on all child items.



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

def process(parent_catalog_item, options = {})
  messages = []
  catalog_items_updated = []
  catalog_items_failed = []
  parent_catalog_item.dependent_catalog_items(options[:target_catalog_id]).each do |child_catalog_item|
    # For now time based updates are ignored
    # next unless options[:ignore_out_of_date_check] || child_catalog_item.price_out_of_date?
    parent_amount = parent_catalog_item.amount

    # Determine discount to use
    discount = child_catalog_item.parent_catalog_discount || 0.0
    unless discount >= 0.0 and discount <= 1.0
      catalog_items_failed << child_catalog_item
      messages << "Catalog Item #{child_catalog_item.id} not updated, calculated discount must be between 0.0 and 1.0"
      next
    end

    target_amount = parent_amount * (1.0 - discount)

    if child_catalog_item.update(amount: target_amount, price_updated_at: Time.current)
      messages << "Child catalog item #{child_catalog_item.id} price updated"
      catalog_items_updated << child_catalog_item
    else
      messages += "Error updating Child catalog item #{child_catalog_item.id} price, #{child_catalog_item.errors_to_s}"
      catalog_items_failed << child_catalog_item
    end
  end
  Result.new(all_price_pushed: catalog_items_failed.empty?,
             messages:,
             catalog_items_updated:,
             catalog_items_failed:)
end