Class: Spree::InventoryUnit
- Inherits:
-
Base
- Object
- ApplicationRecord
- Base
- Spree::InventoryUnit
show all
- 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, page, spree_base_scopes
#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
51
52
53
54
55
|
# File 'app/models/spree/inventory_unit.rb', line 51
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
57
58
59
|
# File 'app/models/spree/inventory_unit.rb', line 57
def self.finalize_units!
update_all(pending: false, updated_at: Time.current)
end
|
.split(original_inventory_unit, extract_quantity) ⇒ Object
66
67
68
69
70
71
|
# File 'app/models/spree/inventory_unit.rb', line 66
def self.split(original_inventory_unit, )
split = original_inventory_unit.dup
split.quantity =
original_inventory_unit.quantity -=
split
end
|
Instance Method Details
#additional_tax_total ⇒ Object
96
97
98
|
# File 'app/models/spree/inventory_unit.rb', line 96
def additional_tax_total
line_item.additional_tax_total * percentage_of_line_item
end
|
#current_or_new_return_item ⇒ Object
#exchanged_unit? ⇒ Boolean
114
115
116
|
# File 'app/models/spree/inventory_unit.rb', line 114
def exchanged_unit?
original_return_item_id?
end
|
83
84
85
|
# File 'app/models/spree/inventory_unit.rb', line 83
def
split_inventory!(1)
end
|
#find_stock_item ⇒ Object
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_total ⇒ Object
100
101
102
|
# File 'app/models/spree/inventory_unit.rb', line 100
def included_tax_total
line_item.included_tax_total * percentage_of_line_item
end
|
#required_quantity ⇒ Object
104
105
106
107
108
109
110
111
112
|
# File 'app/models/spree/inventory_unit.rb', line 104
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
74
75
76
77
78
79
80
81
|
# File 'app/models/spree/inventory_unit.rb', line 74
def split_inventory!()
split = self.class.split(self, )
transaction do
split.save!
save!
end
split
end
|
#variant ⇒ Object
Remove variant default_scope ‘deleted_at: nil`
88
89
90
|
# File 'app/models/spree/inventory_unit.rb', line 88
def variant
Spree::Variant.unscoped { super }
end
|