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|
fulfilled = package.contents.select { |content|
begin
content.variant.suppliers.count == 0
rescue => e
end
}
split_packages << build_package(fulfilled)
supplier_contents = package.contents.select { |content| content.variant.suppliers.count > 0 }
supplier_contents.each do |content|
variant = content.variant
suppliers = variant.supplier_variants.order("spree_supplier_variants.cost ASC").map(&:supplier)
available_supplier = suppliers.detect do |supplier|
supplier.stock_locations_with_available_stock_items(variant).any?
end
stock_location = available_supplier.stock_locations_with_available_stock_items(variant).first
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
|