Class: Spree::Stock::Splitter::Marketplace

Inherits:
Base
  • Object
show all
Defined in:
app/models/spree/stock/splitter/marketplace.rb

Instance Method Summary collapse

Instance Method Details

#split(packages) ⇒ Object



6
7
8
9
10
11
12
13
14
15
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
# File 'app/models/spree/stock/splitter/marketplace.rb', line 6

def split(packages)
  begin
  split_packages = []
  packages.each do |package|
    # Package fulfilled items together.
    fulfilled = package.contents.select { |content|
      begin
      content.variant.suppliers.count == 0
      rescue => e
      end
    }
    split_packages << build_package(fulfilled)
    # Determine which supplier to package shipped items.
    supplier_contents = package.contents.select { |content| content.variant.suppliers.count > 0 }
    supplier_contents.each do |content|
      # Select the related variant
      variant = content.variant
      # Select suppliers ordering ascending according to cost.
      suppliers = variant.supplier_variants.order("spree_supplier_variants.cost ASC").map(&:supplier)
      # Select first supplier that has stock location with avialable stock item.
      available_supplier = suppliers.detect do |supplier|
        supplier.stock_locations_with_available_stock_items(variant).any?
      end
      # Select the first available stock location with in the available_supplier stock locations.
      stock_location = available_supplier.stock_locations_with_available_stock_items(variant).first
      # Add to any existing packages or create a new one.
      if existing_package = split_packages.detect { |p| p.stock_location == stock_location }
        existing_package.contents << content
      else
        split_packages << Spree::Stock::Package.new(stock_location, [content])
      end
    end
  end
  rescue => e
  end
  return_next split_packages
end