Class: Spree::InventoryUnit

Inherits:
Base
  • Object
show all
Defined in:
app/models/spree/inventory_unit.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

page

Methods included from Preferences::Preferable

#clear_preferences, #default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference

Class Method Details

.backordered_for_stock_item(stock_item) ⇒ Object

This was refactored from a simpler query because the previous implementation led to issues once users tried to modify the objects returned. That’s due to ActiveRecord ‘joins(shipment: :stock_location)` only returning readonly objects

Returns an array of backordered inventory units as per a given stock item



46
47
48
49
50
# File 'app/models/spree/inventory_unit.rb', line 46

def self.backordered_for_stock_item(stock_item)
  backordered_per_variant(stock_item).select do |unit|
    unit.shipment.stock_location == stock_item.stock_location
  end
end

.finalize_units!(inventory_units) ⇒ Object



52
53
54
55
56
57
58
59
# File 'app/models/spree/inventory_unit.rb', line 52

def self.finalize_units!(inventory_units)
  inventory_units.map do |iu|
    iu.update_columns(
      pending: false,
      updated_at: Time.now,
    )
  end
end

Instance Method Details

#additional_tax_totalObject



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

def additional_tax_total
  line_item.additional_tax_total * percentage_of_line_item
end

#current_or_new_return_itemObject



71
72
73
# File 'app/models/spree/inventory_unit.rb', line 71

def current_or_new_return_item
  Spree::ReturnItem.from_inventory_unit(self)
end

#find_stock_itemObject



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

def find_stock_item
  Spree::StockItem.where(stock_location_id: shipment.stock_location_id,
    variant_id: variant_id).first
end

#included_tax_totalObject



79
80
81
# File 'app/models/spree/inventory_unit.rb', line 79

def included_tax_total
  line_item.included_tax_total * percentage_of_line_item
end

#variantObject

Remove variant default_scope ‘deleted_at: nil`



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

def variant
  Spree::Variant.unscoped { super }
end