Class: Spree::Price

Inherits:
Object
  • Object
show all
Extended by:
DisplayMoney
Includes:
VatPriceCalculation, Webhooks::HasWebhooks
Defined in:
app/models/spree/price.rb

Constant Summary collapse

MAXIMUM_AMOUNT =
BigDecimal('99_999_999.99')

Instance Method Summary collapse

Methods included from DisplayMoney

money_methods

Methods included from VatPriceCalculation

#gross_amount

Instance Method Details

#amount=(amount) ⇒ Object Also known as: price=



61
62
63
# File 'app/models/spree/price.rb', line 61

def amount=(amount)
  self[:amount] = Spree::LocalizedNumber.parse(amount)
end

#compare_at_amount=(value) ⇒ Object Also known as: compare_at_price=



69
70
71
72
73
# File 'app/models/spree/price.rb', line 69

def compare_at_amount=(value)
  calculated_value = Spree::LocalizedNumber.parse(value) if value.present?

  self[:compare_at_amount] = calculated_value
end

#compare_at_moneyObject



65
66
67
# File 'app/models/spree/price.rb', line 65

def compare_at_money
  Spree::Money.new(compare_at_amount || 0, currency: currency)
end

#compare_at_price_including_vat_for(price_options) ⇒ Object



85
86
87
88
# File 'app/models/spree/price.rb', line 85

def compare_at_price_including_vat_for(price_options)
  options = price_options.merge(tax_category: variant.tax_category)
  gross_amount(compare_at_price, options)
end

#discounted?Boolean

returns true if the price is discounted

Returns:

  • (Boolean)


108
109
110
# File 'app/models/spree/price.rb', line 108

def discounted?
  compare_at_amount.to_i.positive? && compare_at_amount > amount
end

#display_compare_at_price_including_vat_for(price_options) ⇒ Object



94
95
96
# File 'app/models/spree/price.rb', line 94

def display_compare_at_price_including_vat_for(price_options)
  Spree::Money.new(compare_at_price_including_vat_for(price_options), currency: currency)
end

#display_price_including_vat_for(price_options) ⇒ Object



90
91
92
# File 'app/models/spree/price.rb', line 90

def display_price_including_vat_for(price_options)
  Spree::Money.new(price_including_vat_for(price_options), currency: currency)
end

#moneyObject



57
58
59
# File 'app/models/spree/price.rb', line 57

def money
  Spree::Money.new(amount || 0, currency: currency.upcase)
end

#nameString

returns the name of the price in a format of variant name and currency

Returns:

  • (String)


101
102
103
# File 'app/models/spree/price.rb', line 101

def name
  "#{variant.name} - #{currency.upcase}"
end

#non_zero?Boolean

returns true if the price is not zero

Returns:

  • (Boolean)


129
130
131
# File 'app/models/spree/price.rb', line 129

def non_zero?
  !zero?
end

#price_including_vat_for(price_options) ⇒ Object



80
81
82
83
# File 'app/models/spree/price.rb', line 80

def price_including_vat_for(price_options)
  options = price_options.merge(tax_category: variant.tax_category)
  gross_amount(price, options)
end

#was_discounted?Boolean

returns true if the price was discounted

Returns:

  • (Boolean)


115
116
117
# File 'app/models/spree/price.rb', line 115

def was_discounted?
  compare_at_amount_was.to_i.positive? && compare_at_amount_was > amount_was
end

#zero?Boolean

returns true if the price is zero

Returns:

  • (Boolean)


122
123
124
# File 'app/models/spree/price.rb', line 122

def zero?
  amount.nil? || amount.zero?
end