Class: Gemgento::Price

Inherits:
Object
  • Object
show all
Defined in:
app/services/gemgento/price.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(product, user_group = nil, store = nil, quantity = 1.0) ⇒ Price

Returns a new instance of Price.

Parameters:



10
11
12
13
14
15
# File 'app/services/gemgento/price.rb', line 10

def initialize(product, user_group = nil, store = nil, quantity = 1.0)
  @product = product
  @store = store || Gemgento::Store.current
  @user_group = user_group
  @quantity = quantity
end

Instance Attribute Details

#productObject

Returns the value of attribute product.



4
5
6
# File 'app/services/gemgento/price.rb', line 4

def product
  @product
end

#quantityObject

Returns the value of attribute quantity.



4
5
6
# File 'app/services/gemgento/price.rb', line 4

def quantity
  @quantity
end

#storeObject

Returns the value of attribute store.



4
5
6
# File 'app/services/gemgento/price.rb', line 4

def store
  @store
end

#user_groupObject

Returns the value of attribute user_group.



4
5
6
# File 'app/services/gemgento/price.rb', line 4

def user_group
  @user_group
end

Instance Method Details

#calculateObject



17
18
19
20
21
22
23
24
25
26
27
# File 'app/services/gemgento/price.rb', line 17

def calculate
  return gift_price if product.magento_type == 'giftvoucher'

  prices = []
  prices << product.original_price(store)
  prices << product.attribute_value('special_price', store).to_f if has_special?
  prices << Gemgento::PriceRule.calculate_price(product, user_group, store)
  prices << Gemgento::PriceTier.calculate_price(product, quantity, user_group, store)

  return prices.min
end

#gift_priceBigDecimal

If product is a gift, determine gift value.

Returns:

  • (BigDecimal)


32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/services/gemgento/price.rb', line 32

def gift_price
  store = Gemgento::Store.current if store.nil?

  case product.attribute_value('gift_price_type', store)
    when 'Fixed number'
      return product.attribute_value('gift_price', store).to_d
    when 'Percent of Gift Card value'
      return product.attribute_value('gift_value', store).to_d * (product.attribute_value('gift_price', store).to_d / 100.0)
    else
      return product.attribute_value('gift_value', store).to_d
  end
end

#has_special?Boolean

Determine if a product has a valid special price set.

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/services/gemgento/price.rb', line 48

def has_special?
  special_price = product.attribute_value('special_price', store)
  special_from_date = product.attribute_value('special_from_date', store)
  special_to_date = product.attribute_value('special_to_date', store)

  if special_price.nil? # no special price
    return false
  elsif special_from_date.nil? && special_to_date.nil?
    return true
  elsif special_from_date.nil? && !special_to_date.nil?
    return Date.parse(special_to_date) >= Date.today
  elsif special_to_date.nil? && !special_from_date.nil?
    return Date.parse(special_from_date) <= Date.today
  else
    return Date.parse(special_from_date) <= Date.today && Date.parse(special_to_date) >= Date.today
  end
end