Class: Spree::OrderInventoryAssembly

Inherits:
OrderInventory
  • Object
show all
Defined in:
app/models/spree/order_inventory_assembly.rb

Overview

This class has basically the same functionality of Spree core OrderInventory except that it takes account of bundle parts and properly creates and removes inventory unit for each parts of a bundle

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line_item) ⇒ OrderInventoryAssembly

Returns a new instance of OrderInventoryAssembly.



10
11
12
13
14
# File 'app/models/spree/order_inventory_assembly.rb', line 10

def initialize(line_item)
  @order = line_item.order
  @line_item = line_item
  @product = line_item.product
end

Instance Attribute Details

#productObject (readonly)

Returns the value of attribute product.



8
9
10
# File 'app/models/spree/order_inventory_assembly.rb', line 8

def product
  @product
end

Instance Method Details

#verify(shipment = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/spree/order_inventory_assembly.rb', line 16

def verify(shipment = nil)
  if order.completed? || shipment.present?
    line_item.quantity_by_variant.each do |part, total_parts|
      existing_parts = line_item.inventory_units.where(variant: part).count

      self.variant = part

      if existing_parts < total_parts
        if method(:determine_target_shipment).arity == 1
          quantity = total_parts - existing_parts
          shipment = determine_target_shipment(quantity) unless shipment
          add_to_shipment(shipment, quantity)
        else
          shipment = determine_target_shipment unless shipment
          add_to_shipment(shipment, total_parts - existing_parts)
        end
      elsif existing_parts > total_parts
        quantity = existing_parts - total_parts
        if shipment.present?
          remove_from_shipment(shipment, quantity)
        else
          order.shipments.each do |shipment|
            break if quantity == 0

            quantity -= remove_from_shipment(shipment, quantity)
          end
        end
      end
    end
  end
end