Sha256: 2b84d7cd527ae7e954f1e77527622f5bec3738899c288abf70dc22842ff2ccb7

Contents?: true

Size: 1.85 KB

Versions: 4

Compression:

Stored size: 1.85 KB

Contents

module Spree
  module Stock
    module Splitter
      class DropShip < Spree::Stock::Splitter::Base

        def split(packages)
          split_packages = []
          packages.each do |package|
            # Package fulfilled items together.
            fulfilled = package.contents.select { |content| content.variant.suppliers.count == 0 }
            split_packages << build_package(fulfilled)
            # Determine which supplier to package drop shipped items.
            drop_ship = package.contents.select { |content| content.variant.suppliers.count > 0 }
            drop_ship.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 = nil
              if available_supplier.present?
                stock_location = available_supplier.stock_locations_with_available_stock_items(variant).first
              end
              # 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
          return_next split_packages
        end

      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
spree_drop_ship-3.1.3 app/models/spree/stock/splitter/drop_ship.rb
spree_drop_ship-3.1.2 app/models/spree/stock/splitter/drop_ship.rb
spree_drop_ship-3.1.1 app/models/spree/stock/splitter/drop_ship.rb
spree_drop_ship-3.1.0.1 app/models/spree/stock/splitter/drop_ship.rb