Class: Spree::Stock::Adjuster

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/stock/adjuster.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inventory_unit) ⇒ Adjuster

Returns a new instance of Adjuster.



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

def initialize(inventory_unit)
  self.required_quantity = inventory_unit.required_quantity
  self.backorder_package = nil
  self.backorder_item = nil
  self.received_quantity = 0
end

Instance Attribute Details

#backorder_itemObject

Returns the value of attribute backorder_item.



6
7
8
# File 'app/models/spree/stock/adjuster.rb', line 6

def backorder_item
  @backorder_item
end

#backorder_packageObject

Returns the value of attribute backorder_package.



6
7
8
# File 'app/models/spree/stock/adjuster.rb', line 6

def backorder_package
  @backorder_package
end

#received_quantityObject

Returns the value of attribute received_quantity.



6
7
8
# File 'app/models/spree/stock/adjuster.rb', line 6

def received_quantity
  @received_quantity
end

#required_quantityObject

Returns the value of attribute required_quantity.



6
7
8
# File 'app/models/spree/stock/adjuster.rb', line 6

def required_quantity
  @required_quantity
end

Instance Method Details

#adjust(package_to_adjust, item) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/spree/stock/adjuster.rb', line 16

def adjust(package_to_adjust, item)
  if fulfilled?
    package_to_adjust.remove_item item
  elsif item.backordered?
    # We only use the first backorder item to fill backorders
    # as the items/packages are processed in priority order
    if backorder_package.nil?
      self.backorder_package = package_to_adjust
      self.backorder_item = item
    else
      package_to_adjust.remove_item item
    end
  else
    if item.quantity >= remaining_quantity
      item.quantity = remaining_quantity
    end
    self.received_quantity += item.quantity
    update_backorder
  end
end

#fulfilled?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'app/models/spree/stock/adjuster.rb', line 46

def fulfilled?
  remaining_quantity.zero?
end

#remaining_quantityObject



50
51
52
# File 'app/models/spree/stock/adjuster.rb', line 50

def remaining_quantity
  required_quantity - received_quantity
end

#update_backorderObject



37
38
39
40
41
42
43
44
# File 'app/models/spree/stock/adjuster.rb', line 37

def update_backorder
  return if backorder_package.nil?
  if fulfilled?
    backorder_package.remove_item backorder_item
  elsif backorder_item.present?
    backorder_item.quantity = remaining_quantity
  end
end