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

#process(parent_catalog_item, options = {}) ⇒ Result

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.

Parameters:

  • parent_catalog_item (CatalogItem)

    the parent catalog item whose price is pushed

  • options (Hash) (defaults to: {})

    processing options

Options Hash (options):

  • target_catalog_id (Integer)

    restrict the push to children in this catalog (defaults to all dependent catalogs)

Returns:



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

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) && (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