Class: Spree::LineItem

Inherits:
Object
  • Object
show all
Extended by:
DisplayMoney
Includes:
HasCustomFields, HasExternalReferences, Metadata
Defined in:
app/models/spree/line_item.rb

Constant Summary collapse

DB_INTEGER_MAX =
(2**31) - 1
MANUAL_PRICE_SOURCE =

The price_source value marking a negotiated (admin-set) unit price. Reserved: no pricing provider may register it as its key — see Spree::PricingProvider.verify_registry!.

Spree::PricingProvider::MANUAL_PRICE_SOURCE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DisplayMoney

money_methods

Methods included from Metadata

#private_metadata, #private_metadata=

Methods included from HasExternalReferences

#assign_external_references, #external_id_for, #set_external_id

Instance Attribute Details

#price_resolved ⇒ Object

Set by Spree::Carts::PriceItems when a workflow has already resolved this line's price outside the transaction. The quantity-change callback then stands down rather than asking the provider a second time from inside the save — which is the network-call-in-transaction the workflow split exists to prevent, and would overwrite the fresher answer.



48
49
50
# File 'app/models/spree/line_item.rb', line 48

def price_resolved
  @price_resolved
end

#skip_tax_estimation ⇒ Object

Set on copies whose tax is already known — the completion copy carries the cart's tax rows over verbatim, so re-estimating would discard the answer and, with an external engine, spend a remote call per line item.



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

def skip_tax_estimation
  @skip_tax_estimation
end

#target_fulfillment ⇒ Object

Pins inventory-unit placement to a specific fulfillment when set (admin add-to-this-fulfillment flows).



107
108
109
# File 'app/models/spree/line_item.rb', line 107

def target_fulfillment
  @target_fulfillment
end

Instance Method Details

#amount ⇒ BigDecimal Also known as: subtotal

Returns the amount (price * quantity) of the line item

Returns:

  • (BigDecimal)


228
229
230
# File 'app/models/spree/line_item.rb', line 228

def amount
  Spree::Money::Rounding.to_currency(price * quantity, currency)
end

#any_shipped? ⇒ Boolean

returns true if any of the inventory units are shipped

Returns:

  • (Boolean)


316
317
318
# File 'app/models/spree/line_item.rb', line 316

def any_shipped?
  fulfillment_items.any?(&:shipped?)
end

#compare_at_amount ⇒ BigDecimal

Returns the compare at amount (compare at price * quantity) of the line item

Returns:

  • (BigDecimal)


235
236
237
# File 'app/models/spree/line_item.rb', line 235

def compare_at_amount
  (variant.compare_at_amount_in(currency) || 0) * quantity
end

#copy_catalog_price ⇒ void

This method returns an undefined value.

Assigns the price the local catalog gives for this line, with no pricing provider consulted — the fallback for a line built outside a workflow (seeds, console, an extension) that would otherwise be saved with no price at all. Assigns without saving; the caller is mid-save.



173
174
175
176
177
178
179
180
181
# File 'app/models/spree/line_item.rb', line 173

def copy_catalog_price
  context = Spree::Pricing::Context.from_order(variant, owner, quantity: quantity)
  price_record = Spree::PricingProvider::Internal.new.price_for(context)
  return if price_record.blank?

  # Through the same writer every resolved price goes through, so a line
  # built here is priced exactly like a resolved one.
  Spree::Carts::PriceItems.apply([[self, price_record]], persist: false)
end

#copy_price ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
# File 'app/models/spree/line_item.rb', line 147

def copy_price
  if variant
    # Local catalog only: this runs inside a save, and the workflows have
    # already priced the line through the provider before the transaction
    # opened. Reaching a provider from a validation callback would put a
    # network call inside every line-item save.
    copy_catalog_price if price.nil?
    self.cost_price = variant.cost_price if cost_price.nil?
    self.currency = owner.currency if currency.nil? && owner
  end
end

#copy_seller ⇒ Object

Reads the seller off the variant, which resolves it — its own when the variant carries one, otherwise the product's.

Only filled while the line is still being chosen — a cart, or an admin draft that has not been placed. Once the order is placed the line records who sold it, and nothing about the catalog changing hands afterwards may rewrite that, including a first-party line whose nil is just as much a record as a seller's id.

Keyed on the order being placed rather than on the line being persisted: a draft order's lines are persisted too, and they must keep following their product until the order is actually placed.



199
200
201
202
203
204
# File 'app/models/spree/line_item.rb', line 199

def copy_seller
  return if variant.blank?
  return if order&.placed?

  self.seller_id = variant.resolved_seller_id
end

#copy_tax_category ⇒ Object



183
184
185
# File 'app/models/spree/line_item.rb', line 183

def copy_tax_category
  self.tax_category = variant.tax_category if variant
end

#delivery_cost ⇒ BigDecimal

This line item's share of its fulfillments' delivery cost, split proportionally by unit count.

Returns:

  • (BigDecimal)


331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'app/models/spree/line_item.rb', line 331

def delivery_cost
  # distinct: the has_many :through yields one row per fulfillment item,
  # so a fulfillment holding several of this line item's units would
  # otherwise be counted once per unit.
  fulfillments.distinct.sum do |fulfillment|
    # next, never return — a return here would abandon the whole sum, so
    # one skippable fulfillment would zero out every other one.
    next BigDecimal('0') if fulfillment.canceled? || fulfillment.cost.zero?

    units = fulfillment.fulfillment_items
    next BigDecimal('0') if units.empty?

    line_item_units = units.count { |unit| unit.line_item_id == id }
    next BigDecimal('0') if line_item_units.zero?

    fulfillment.cost * (line_item_units.to_d / units.count)
  end
end

#discounted_amount_for(units) ⇒ BigDecimal

What units of this line cost the customer after discounts. Multiplied before dividing, so all of a line's units together come to the whole line.

Parameters:

  • units (Integer)

Returns:

  • (BigDecimal) —

    rounded to the currency, never negative



263
264
265
266
267
# File 'app/models/spree/line_item.rb', line 263

def discounted_amount_for(units)
  return BigDecimal(0) if quantity.to_i.zero?

  Spree::Money::Rounding.to_currency([discounted_amount, 0].max * units.to_i / quantity, currency)
end

#discounted_price ⇒ Object



219
220
221
222
223
# File 'app/models/spree/line_item.rb', line 219

def discounted_price
  return price if quantity.zero?

  price - (discount_total.abs / quantity)
end

#display_shipping_cost ⇒ Object

Deprecated.

Use #display_delivery_cost; removed in 6.3.



357
358
359
360
361
362
# File 'app/models/spree/line_item.rb', line 357

def display_shipping_cost
  Spree::Deprecation.warn(
    'Spree::LineItem#display_shipping_cost is deprecated and will be removed in Spree 6.3. Use #display_delivery_cost instead.'
  )
  display_delivery_cost
end

#final_amount ⇒ BigDecimal Also known as: total

Returns the final amount of the line item

Returns:

  • (BigDecimal)


281
282
283
# File 'app/models/spree/line_item.rb', line 281

def final_amount
  amount + adjustment_total
end

#fully_shipped? ⇒ Boolean

returns true if all of the inventory units are shipped

Returns:

  • (Boolean)


323
324
325
# File 'app/models/spree/line_item.rb', line 323

def fully_shipped?
  fulfillment_items.all?(&:shipped?)
end

#insufficient_stock? ⇒ Boolean

Returns true if the line item has insufficient stock

Returns:

  • (Boolean)


309
310
311
# File 'app/models/spree/line_item.rb', line 309

def insufficient_stock?
  !sufficient_stock?
end

#item_weight ⇒ BigDecimal

Returns the weight of the line item

Returns:

  • (BigDecimal)


288
289
290
# File 'app/models/spree/line_item.rb', line 288

def item_weight
  variant.weight * quantity
end

#manual_price? ⇒ Boolean

Whether this line carries a negotiated price set by staff. A manual price is never overwritten by repricing — only the explicit revert (+price: nil+ through the admin item update) clears it.

Returns:

  • (Boolean)


394
395
396
# File 'app/models/spree/line_item.rb', line 394

def manual_price?
  price_source == MANUAL_PRICE_SOURCE
end

#maximum_quantity ⇒ Integer

Returns the maximum quantity that can be added to the line item

Returns:

  • (Integer)


378
379
380
# File 'app/models/spree/line_item.rb', line 378

def maximum_quantity
  @maximum_quantity ||= variant.backorderable? ? DB_INTEGER_MAX : variant.total_on_hand
end

#options=(options = {}) ⇒ Object



364
365
366
367
368
369
370
371
372
373
# File 'app/models/spree/line_item.rb', line 364

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

  opts = options.dup # we will be deleting from the hash, so leave the caller's copy intact

  currency = opts.delete(:currency) || owner&.currency

  update_price_from_modifier(currency, opts)
  assign_attributes opts
end

#order=(record) ⇒ Object

Bridge for legacy callers assigning current_order (now a Spree::Cart) to the order association — routes carts to the cart FK instead.



138
139
140
141
142
143
144
145
# File 'app/models/spree/line_item.rb', line 138

def order=(record)
  if record.is_a?(Spree::Cart)
    self.cart = record
    super(nil)
  else
    super
  end
end

#owner ⇒ Spree::Cart, ...

The exactly-one owner of this line item — the cart during checkout, the order after completion. New code must read owner, never assume order.

Returns:



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

def owner
  order || cart
end

#recalculate_price ⇒ void

This method returns an undefined value.

Recalculates and persists the price based on the current quantity and pricing context — volume breaks, price list rules, contract pricing.

A thin delegate to Carts::PriceItems, which is where a store's pricing provider is consulted. Prefer calling that service directly for anything touching more than one line: it asks the provider once for the batch, while this asks once per item.



407
408
409
# File 'app/models/spree/line_item.rb', line 407

def recalculate_price
  reprice(persist: true)
end

#shipping_cost ⇒ Object

Deprecated.

Use #delivery_cost; removed in 6.3.



351
352
353
354
# File 'app/models/spree/line_item.rb', line 351

def shipping_cost
  Spree::Deprecation.warn('Spree::LineItem#shipping_cost is deprecated and will be removed in Spree 6.3. Use #delivery_cost instead.')
  delivery_cost
end

#sufficient_stock? ⇒ Boolean

Returns true if the line item has sufficient stock

The owner's own active stock reservations are excluded from the availability check — a customer's own checkout hold must not make their own line item look out of stock.

Returns:

  • (Boolean)


302
303
304
# File 'app/models/spree/line_item.rb', line 302

def sufficient_stock?
  Spree::Stock::Quantifier.new(variant, excluded_order: owner).can_supply?(quantity)
end

#target_shipment ⇒ Object

Deprecated.

Use #target_fulfillment; removed in 6.3.



110
111
112
113
# File 'app/models/spree/line_item.rb', line 110

def target_shipment
  Spree::Deprecation.warn('Spree::LineItem#target_shipment is deprecated and will be removed in Spree 6.3. Use #target_fulfillment instead.')
  target_fulfillment
end

#target_shipment=(value) ⇒ Object

Deprecated.

Use #target_fulfillment=; removed in 6.3.



116
117
118
119
# File 'app/models/spree/line_item.rb', line 116

def target_shipment=(value)
  Spree::Deprecation.warn('Spree::LineItem#target_shipment= is deprecated and will be removed in Spree 6.3. Use #target_fulfillment= instead.')
  self.target_fulfillment = value
end

#tax_total ⇒ BigDecimal

returns the total tax amount

Returns:

  • (BigDecimal)


251
252
253
# File 'app/models/spree/line_item.rb', line 251

def tax_total
  included_tax_total + additional_tax_total
end

#taxable_amount ⇒ BigDecimal Also known as: discounted_amount

Returns the taxable amount (amount + taxable adjustment total) of the line item

Returns:

  • (BigDecimal)


244
245
246
# File 'app/models/spree/line_item.rb', line 244

def taxable_amount
  amount + taxable_adjustment_total
end

#taxable_basis ⇒ BigDecimal

Returns the amount this line item is taxed on. Whole-order promotions are distributed to line-item Discount rows at application time, so the discounted amount already carries the line's share. Never negative.

Returns:

  • (BigDecimal)


274
275
276
# File 'app/models/spree/line_item.rb', line 274

def taxable_basis
  [taxable_amount, BigDecimal(0)].max
end

#thumbnail ⇒ Spree::Media?

Returns the thumbnail image for this line item Prefers variant primary media, falls back to product primary media

Returns:



97
# File 'app/models/spree/line_item.rb', line 97

delegate :thumbnail, to: :variant

#update_price ⇒ Object

Assigns the current price without saving — the caller is expected to. #recalculate_price persists instead. persist: false — runs mid-build and from callers that save the record themselves.



163
164
165
# File 'app/models/spree/line_item.rb', line 163

def update_price
  reprice(persist: false)
end

#with_digital_assets? ⇒ Boolean

Returns true if the line item variant has digital assets

Returns:

  • (Boolean)


385
386
387
# File 'app/models/spree/line_item.rb', line 385

def with_digital_assets?
  variant.with_digital_assets?
end