mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-04-06 07:29:16 +00:00
51 lines
1.4 KiB
Ruby
51 lines
1.4 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Spree
|
|
module Stock
|
|
class Packer
|
|
attr_reader :stock_location, :order, :splitters, :package_factory
|
|
|
|
def initialize(stock_location, order, splitters = [OrderManagement::Stock::BasicSplitter])
|
|
@stock_location = stock_location
|
|
@order = order
|
|
@splitters = splitters
|
|
@package_factory = Spree::Config.package_factory
|
|
end
|
|
|
|
def packages
|
|
if splitters.empty?
|
|
[default_package]
|
|
else
|
|
build_splitter.split [default_package]
|
|
end
|
|
end
|
|
|
|
def default_package
|
|
package = package_factory.new(stock_location, order)
|
|
order.line_items.each do |line_item|
|
|
if Config.track_inventory_levels
|
|
next unless stock_location.stock_item(line_item.variant)
|
|
|
|
on_hand, backordered = stock_location.fill_status(line_item.variant, line_item.quantity)
|
|
package.add line_item.variant, on_hand, :on_hand if on_hand.positive?
|
|
package.add line_item.variant, backordered, :backordered if backordered.positive?
|
|
else
|
|
package.add line_item.variant, line_item.quantity, :on_hand
|
|
end
|
|
end
|
|
package
|
|
end
|
|
|
|
private
|
|
|
|
def build_splitter
|
|
splitter = nil
|
|
splitters.reverse.each do |klass|
|
|
splitter = klass.new(self, splitter)
|
|
end
|
|
splitter
|
|
end
|
|
end
|
|
end
|
|
end
|