Class: Spree::InventoryUnit
- Defined in:
- app/models/spree/inventory_unit.rb
Overview
Tracks the state of line items’ fulfillment.
Constant Summary collapse
- PRE_SHIPMENT_STATES =
%w(backordered on_hand)
- POST_SHIPMENT_STATES =
%w(returned)
- CANCELABLE_STATES =
['on_hand', 'backordered', 'shipped']
Class Method Summary collapse
-
.finalize_units!(inventory_units) ⇒ Object
Updates the given inventory units to not be pending.
Instance Method Summary collapse
-
#additional_tax_total ⇒ BigDecimal
The portion of the additional tax on the line item this inventory unit belongs to that is associated with this individual inventory unit.
- #allow_ship? ⇒ Boolean
-
#backordered_for_stock_item(stock_item) ⇒ ActiveRecord::Relation<Spree::InventoryUnit>
Backordered inventory units for the given stock item.
-
#current_or_new_return_item ⇒ Spree::ReturnItem
A valid return item for this inventory unit if one exists, or a new one if one does not.
-
#exchange_requested? ⇒ Boolean
True if this inventory unit has any return items which have requested exchanges.
-
#find_stock_item ⇒ Spree::StockItem
The first stock item from this shipment’s stock location that is associated with this inventory unit’s variant.
-
#included_tax_total ⇒ BigDecimal
The portion of the included tax on the line item this inventory unit belongs to that is associated with this individual inventory unit.
- #order=(_) ⇒ Object
Methods inherited from Base
display_includes, #initialize_preference_defaults, page, preference
Methods included from Preferences::Preferable
#admin_form_preference_names, #default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference
Class Method Details
.finalize_units!(inventory_units) ⇒ Object
Updates the given inventory units to not be pending.
86 87 88 89 90 91 92 93 |
# File 'app/models/spree/inventory_unit.rb', line 86 def self.finalize_units!(inventory_units) inventory_units.map do |iu| iu.update_columns( pending: false, updated_at: Time.current ) end end |
Instance Method Details
#additional_tax_total ⇒ BigDecimal
Returns the portion of the additional tax on the line item this inventory unit belongs to that is associated with this individual inventory unit.
111 112 113 |
# File 'app/models/spree/inventory_unit.rb', line 111 def additional_tax_total line_item.additional_tax_total * percentage_of_line_item end |
#allow_ship? ⇒ Boolean
128 129 130 |
# File 'app/models/spree/inventory_unit.rb', line 128 def allow_ship? on_hand? end |
#backordered_for_stock_item(stock_item) ⇒ ActiveRecord::Relation<Spree::InventoryUnit>
Returns backordered inventory units for the given stock item.
55 56 57 58 |
# File 'app/models/spree/inventory_unit.rb', line 55 scope :backordered_for_stock_item, ->(stock_item) do backordered_per_variant(stock_item) .where(spree_shipments: { stock_location_id: stock_item.stock_location_id }) end |
#current_or_new_return_item ⇒ Spree::ReturnItem
Returns a valid return item for this inventory unit if one exists, or a new one if one does not.
104 105 106 |
# File 'app/models/spree/inventory_unit.rb', line 104 def current_or_new_return_item Spree::ReturnItem.from_inventory_unit(self) end |
#exchange_requested? ⇒ Boolean
Returns true if this inventory unit has any return items which have requested exchanges.
124 125 126 |
# File 'app/models/spree/inventory_unit.rb', line 124 def exchange_requested? return_items.not_expired.any?(&:exchange_requested?) end |
#find_stock_item ⇒ Spree::StockItem
Returns the first stock item from this shipment’s stock location that is associated with this inventory unit’s variant.
97 98 99 100 |
# File 'app/models/spree/inventory_unit.rb', line 97 def find_stock_item Spree::StockItem.where(stock_location_id: shipment.stock_location_id, variant_id: variant_id).first end |
#included_tax_total ⇒ BigDecimal
Returns the portion of the included tax on the line item this inventory unit belongs to that is associated with this individual inventory unit.
118 119 120 |
# File 'app/models/spree/inventory_unit.rb', line 118 def included_tax_total line_item.included_tax_total * percentage_of_line_item end |
#order=(_) ⇒ Object
24 25 26 |
# File 'app/models/spree/inventory_unit.rb', line 24 def order=(_) raise "The order association has been removed from InventoryUnit. The order is now determined from the shipment." end |