Class: Spree::InventoryUnit
- Inherits:
-
Object
- Object
- Spree::InventoryUnit
- Extended by:
- DisplayMoney
- Includes:
- Webhooks::HasWebhooks
- Defined in:
- app/models/spree/inventory_unit.rb
Class Method Summary collapse
-
.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.
- .finalize_units! ⇒ Object
- .split(original_inventory_unit, extract_quantity) ⇒ Object
Instance Method Summary collapse
- #additional_tax_total ⇒ Object
- #charged_amount ⇒ Object
- #current_or_new_return_item ⇒ Object
- #exchanged_unit? ⇒ Boolean
- #extract_singular_inventory! ⇒ Object
- #find_stock_item ⇒ Object
- #included_tax_total ⇒ Object
- #percentage_of_line_item ⇒ Object
- #required_quantity ⇒ Object
-
#split_inventory!(extract_quantity) ⇒ Object
This will fail if extract >= available_quantity.
Methods included from DisplayMoney
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
58 59 60 61 62 |
# File 'app/models/spree/inventory_unit.rb', line 58 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
64 65 66 |
# File 'app/models/spree/inventory_unit.rb', line 64 def self.finalize_units! update_all(pending: false, updated_at: Time.current) end |
.split(original_inventory_unit, extract_quantity) ⇒ Object
72 73 74 75 76 77 |
# File 'app/models/spree/inventory_unit.rb', line 72 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_total ⇒ Object
97 98 99 |
# File 'app/models/spree/inventory_unit.rb', line 97 def additional_tax_total line_item.additional_tax_total * percentage_of_line_item end |
#charged_amount ⇒ Object
119 120 121 |
# File 'app/models/spree/inventory_unit.rb', line 119 def charged_amount percentage_of_line_item * line_item.pre_tax_amount end |
#current_or_new_return_item ⇒ Object
93 94 95 |
# File 'app/models/spree/inventory_unit.rb', line 93 def current_or_new_return_item Spree::ReturnItem.from_inventory_unit(self) end |
#exchanged_unit? ⇒ Boolean
115 116 117 |
# File 'app/models/spree/inventory_unit.rb', line 115 def exchanged_unit? original_return_item_id? end |
#extract_singular_inventory! ⇒ Object
89 90 91 |
# File 'app/models/spree/inventory_unit.rb', line 89 def extract_singular_inventory! split_inventory!(1) end |
#find_stock_item ⇒ Object
68 69 70 |
# File 'app/models/spree/inventory_unit.rb', line 68 def find_stock_item shipment.stock_location.stock_item_or_create(variant) end |
#included_tax_total ⇒ Object
101 102 103 |
# File 'app/models/spree/inventory_unit.rb', line 101 def included_tax_total line_item.included_tax_total * percentage_of_line_item end |
#percentage_of_line_item ⇒ Object
123 124 125 |
# File 'app/models/spree/inventory_unit.rb', line 123 def percentage_of_line_item quantity / BigDecimal(line_item.quantity) end |
#required_quantity ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'app/models/spree/inventory_unit.rb', line 105 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
80 81 82 83 84 85 86 87 |
# File 'app/models/spree/inventory_unit.rb', line 80 def split_inventory!(extract_quantity) split = self.class.split(self, extract_quantity) transaction do split.save! save! end split end |