Class: Spree::LineItem

Inherits:
Base
  • Object
show all
Extended by:
DisplayMoney
Defined in:
app/models/spree/line_item.rb

Overview

Variants placed in the Order at a particular price.

Spree::LineItem is an ActiveRecord model which records which Spree::Variant a customer has chosen to place in their order. It also acts as the permanent record of the customer’s order by recording relevant price, taxation, and inventory concerns. Line items can also have adjustments placed on them as part of the promotion system.

Defined Under Namespace

Classes: CurrencyMismatch

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DisplayMoney

money_methods

Methods inherited from Base

display_includes, page, preference, #preferences

Methods included from Core::Permalinks

#generate_permalink, #save_permalink

Instance Attribute Details

#price_currencyObject

Returns the value of attribute price_currency.



46
47
48
# File 'app/models/spree/line_item.rb', line 46

def price_currency
  @price_currency
end

#target_shipmentObject

Returns the value of attribute target_shipment.



46
47
48
# File 'app/models/spree/line_item.rb', line 46

def target_shipment
  @target_shipment
end

Instance Method Details

#amountBigDecimal Also known as: subtotal



53
54
55
# File 'app/models/spree/line_item.rb', line 53

def amount
  price * quantity
end

#currency=(_currency) ⇒ Object



159
160
161
162
163
# File 'app/models/spree/line_item.rb', line 159

def currency=(_currency)
  Spree::Deprecation.warn 'Spree::LineItem#currency= is deprecated ' \
    'and will take no effect.',
    caller
end

#discounted_amountBigDecimal



60
61
62
# File 'app/models/spree/line_item.rb', line 60

def discounted_amount
  amount + promo_total
end

#insufficient_stock?Boolean



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

def insufficient_stock?
  !sufficient_stock?
end

#money_price=(money) ⇒ Object

Sets price from a Spree::Money object



112
113
114
115
116
117
118
119
120
121
122
# File 'app/models/spree/line_item.rb', line 112

def money_price=(money)
  if !money
    self.price = nil
  elsif money.currency.iso_code != currency && Spree::Config.raise_with_invalid_currency
    line_item_errors = ActiveModel::Errors.new(self)
    raise CurrencyMismatch, line_item_errors.generate_message(:price, :does_not_match_order_currency, locale: :en)
  else
    self.price_currency = money.currency.iso_code
    self.price = money.to_d
  end
end

#options=(options = {}) ⇒ Object

Sets options on the line item and updates the price.

The options can be arbitrary attributes on the LineItem.



141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'app/models/spree/line_item.rb', line 141

def options=(options = {})
  return unless options.present?

  assign_attributes options

  # When price is part of the options we are not going to fetch
  # it from the variant. Please note that this always allows to set
  # a price for this line item, even if there is no existing price
  # for the associated line item in the order currency.
  unless options.key?(:price) || options.key?('price')
    self.money_price = variant.price_for(pricing_options)
  end
end

#pricing_optionsObject



155
156
157
# File 'app/models/spree/line_item.rb', line 155

def pricing_options
  Spree::Config.pricing_options_class.from_line_item(self)
end

#sufficient_stock?Boolean



126
127
128
# File 'app/models/spree/line_item.rb', line 126

def sufficient_stock?
  Stock::Quantifier.new(variant).can_supply? quantity
end

#totalBigDecimal Also known as: final_amount



67
68
69
# File 'app/models/spree/line_item.rb', line 67

def total
  amount + adjustment_total
end

#total_before_taxBigDecimal



75
76
77
# File 'app/models/spree/line_item.rb', line 75

def total_before_tax
  amount + adjustments.select { |value| !value.tax? && value.eligible? }.sum(&:amount)
end

#total_excluding_vatBigDecimal Also known as: pre_tax_amount

Note:

just like amount, this does not include any additional tax

Returns the amount of this line item before VAT tax.



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

def total_excluding_vat
  total_before_tax - included_tax_total
end