Sha256: b1a92c807cebbdc14783afca3958424cda2104cab57b9d7e5f321f22cc36b899

Contents?: true

Size: 1.27 KB

Versions: 3

Compression:

Stored size: 1.27 KB

Contents

module Comable
  module Inventory
    class Coordinator
      attr_accessor :order
      attr_accessor :units

      def initialize(order)
        @order = order
        @units = build_units
      end

      def shipments
        packages.map(&:to_shipment)
      end

      private

      def packages
        packages = build_packages
        adjusted_packages = adjust_packages(packages)
        compact_packages(adjusted_packages)
      end

      def build_packages
        Comable::StockLocation.active.map do |stock_location|
          next unless units_exists_in? stock_location
          build_packer(stock_location).package
        end.compact
      end

      def adjust_packages(packages)
        Adjuster.new(packages, units).adjusted_packages
      end

      def compact_packages(packages)
        packages.reject(&:empty?)
      end

      def units_exists_in?(stock_location)
        stock_location.stocks.where(variant_id: units.map(&:variant).map(&:id).uniq).exists?
      end

      def build_packer(stock_location)
        Packer.new(stock_location, units)
      end

      def build_units
        order.order_items.map do |order_item|
          order_item.quantity.times.map do
            Unit.new(order_item.variant)
          end
        end.flatten
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
comable-core-0.7.1 app/models/comable/inventory/coordinator.rb
comable-core-0.7.0 app/models/comable/inventory/coordinator.rb
comable-core-0.7.0.beta2 app/models/comable/inventory/coordinator.rb