Class: Workarea::Order::Item

Inherits:
Object
  • Object
show all
Includes:
ApplicationDocument
Defined in:
app/models/workarea/order/item.rb

Instance Method Summary collapse

Methods included from ApplicationDocument

#releasable?

Methods included from Sidekiq::Callbacks

assert_valid_config!, async, disable, enable, inline, #run_callbacks

Instance Method Details

#adjust_pricing(options = {}) ⇒ self

Adds a price adjustment to the item. Does not persist.

Returns:

  • (self)


44
45
46
# File 'app/models/workarea/order/item.rb', line 44

def adjust_pricing(options = {})
  price_adjustments.build(options)
end

#current_unit_priceMoney

The unit price of the item including all currently attached price adjustments.

Returns:



84
85
86
87
# File 'app/models/workarea/order/item.rb', line 84

def current_unit_price
  return 0.to_m if price_adjustments.blank?
  price_adjustments.adjusting('item').sum.to_m / quantity
end

#customizationsHash

Customizations to the item, beyond what the variant options were. Examples would include engraving, monogramming, etc. This should be a sanitized hash that has passed through the Catalog::Customizations system.

Returns:

  • (Hash)


107
108
109
# File 'app/models/workarea/order/item.rb', line 107

def customizations
  super || {}
end

#customizations_eql?(test) ⇒ Boolean

Determine whether the customizations of this item are equivalent to the customizations of another. This is used when updating/adding items so we can see whether we should merge items that have the same SKU but different customizations.

Parameters:

Returns:

  • (Boolean)


128
129
130
131
132
133
134
135
136
137
# File 'app/models/workarea/order/item.rb', line 128

def customizations_eql?(test)
  if test.present? && customizations.present?
    test.inject(true) do |memo, tuple|
      key, value = *tuple
      memo && customizations[key].to_s == value.to_s
    end
  else
    test.blank? && customizations.blank?
  end
end

#customized?Boolean

Whether this item has any customizations.

Returns:

  • (Boolean)


115
116
117
# File 'app/models/workarea/order/item.rb', line 115

def customized?
  customizations.present?
end

#digital?Boolean

Whether this item is a digital (not-shipped) type of item.

Returns:

  • (Boolean)


36
37
38
# File 'app/models/workarea/order/item.rb', line 36

def digital?
  !!product_attributes['digital']
end

#matches_categories?(*ids) ⇒ Boolean

Whether the item is in any of the category ids passed. Used in discount qualification.

Parameters:

  • ids (Array)

Returns:

  • (Boolean)


54
55
56
57
# File 'app/models/workarea/order/item.rb', line 54

def matches_categories?(*ids)
  match_ids = Array(ids).flatten.map(&:to_s)
  (category_ids.map(&:to_s) & match_ids).any?
end

#matches_products?(*ids) ⇒ Boolean

Whether the item is for any of the product ids passed. Used in discount qualification.

Parameters:

  • ids (Array)

Returns:

  • (Boolean)


65
66
67
68
# File 'app/models/workarea/order/item.rb', line 65

def matches_products?(*ids)
  match_ids = Array(ids).flatten.map(&:to_s)
  product_id.to_s.in?(match_ids)
end

#on_sale?Boolean

Whether this item is on sale (as of the last time the order was priced).

Returns:

  • (Boolean)


94
95
96
97
# File 'app/models/workarea/order/item.rb', line 94

def on_sale?
  return false if price_adjustments.blank?
  !!price_adjustments.first.data['on_sale']
end

#original_unit_priceMoney

The base price per-unit for this item.

Returns:



74
75
76
77
# File 'app/models/workarea/order/item.rb', line 74

def original_unit_price
  return 0.to_m if price_adjustments.blank?
  price_adjustments.first.unit.to_m
end