Class: Spree::Price

Inherits:
Object
  • Object
show all
Extended by:
DisplayMoney
Includes:
VatPriceCalculation
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=



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

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

#amount_in_centsInteger

Returns the amount in cents

Returns:

  • (Integer)


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

def amount_in_cents
  display_amount&.amount_in_cents
end

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



95
96
97
98
99
# File 'app/models/spree/price.rb', line 95

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

  self[:compare_at_amount] = calculated_value
end

#compare_at_amount_in_centsInteger?

Returns the compare at amount in cents

Returns:

  • (Integer, nil)


111
112
113
114
115
# File 'app/models/spree/price.rb', line 111

def compare_at_amount_in_cents
  return nil if compare_at_amount.nil?

  display_compare_at_amount.amount_in_cents
end

#compare_at_moneyObject



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

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

#compare_at_price_including_vat_for(price_options) ⇒ Object



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

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)


150
151
152
# File 'app/models/spree/price.rb', line 150

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

#display_compare_at_amountSpree::Money?

Returns the compare at amount for display

Returns:



103
104
105
106
107
# File 'app/models/spree/price.rb', line 103

def display_compare_at_amount
  return nil if compare_at_amount.nil?

  Spree::Money.new(compare_at_amount, currency: currency)
end

#display_compare_at_priceObject



56
# File 'app/models/spree/price.rb', line 56

alias display_compare_at_price display_compare_at_amount

#display_compare_at_price_including_vat_for(price_options) ⇒ Object



136
137
138
# File 'app/models/spree/price.rb', line 136

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



132
133
134
# File 'app/models/spree/price.rb', line 132

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

#moneyObject



77
78
79
# File 'app/models/spree/price.rb', line 77

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)


143
144
145
# File 'app/models/spree/price.rb', line 143

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

#non_zero?Boolean

returns true if the price is not zero

Returns:

  • (Boolean)


171
172
173
# File 'app/models/spree/price.rb', line 171

def non_zero?
  !zero?
end

#price_including_vat_for(price_options) ⇒ Object



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

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

#prior_priceSpree::PriceHistory?

Returns the price history record with the lowest amount in the last 30 days Used for EU Omnibus Directive compliance

Returns:



179
180
181
# File 'app/models/spree/price.rb', line 179

def prior_price
  price_histories.where(recorded_at: 30.days.ago..).order(:amount).first
end

#was_discounted?Boolean

returns true if the price was discounted

Returns:

  • (Boolean)


157
158
159
# File 'app/models/spree/price.rb', line 157

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)


164
165
166
# File 'app/models/spree/price.rb', line 164

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