Class: Spree::InventoryUnit

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

belongs_to_required_by_default, for_store, has_many_inversing, json_api_columns, json_api_permitted_attributes, json_api_type, page, spree_base_scopes, spree_base_uniqueness_scope

Methods included from Preferences::Preferable

#clear_preferences, #default_preferences, #defined_preferences, #deprecated_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_deprecated, #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



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

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!Object



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

def self.finalize_units!
  update_all(pending: false, updated_at: Time.current)
end

.split(original_inventory_unit, extract_quantity) ⇒ Object



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

def self.split(original_inventory_unit, extract_quantity)
  split = original_inventory_unit.dup
  split.quantity = extract_quantity
  original_inventory_unit.quantity -= extract_quantity
  split
end

Instance Method Details

#additional_tax_totalObject



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

def additional_tax_total
  line_item.additional_tax_total * percentage_of_line_item
end

#current_or_new_return_itemObject



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

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

#exchanged_unit?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'app/models/spree/inventory_unit.rb', line 112

def exchanged_unit?
  original_return_item_id?
end

#extract_singular_inventory!Object



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

def extract_singular_inventory!
  split_inventory!(1)
end

#find_stock_itemObject



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

def find_stock_item
  shipment.stock_location.stock_item_or_create(variant)
end

#included_tax_totalObject



98
99
100
# File 'app/models/spree/inventory_unit.rb', line 98

def included_tax_total
  line_item.included_tax_total * percentage_of_line_item
end

#required_quantityObject



102
103
104
105
106
107
108
109
110
# File 'app/models/spree/inventory_unit.rb', line 102

def required_quantity
  return @required_quantity unless @required_quantity.nil?

  @required_quantity = if exchanged_unit?
                         original_return_item.return_quantity
                       else
                         line_item.quantity
                       end
end

#split_inventory!(extract_quantity) ⇒ Object

This will fail if extract >= available_quantity



77
78
79
80
81
82
83
84
# File 'app/models/spree/inventory_unit.rb', line 77

def split_inventory!(extract_quantity)
  split = self.class.split(self, extract_quantity)
  transaction do
    split.save!
    save!
  end
  split
end