Simplify packer and coordinator baed on the fact that there's only one stock_location so there will only be one package per order

This commit is contained in:
Luis Ramos
2020-07-02 20:45:01 +01:00
parent ff046f7a6c
commit d323c5bdcb
3 changed files with 22 additions and 44 deletions

View File

@@ -15,24 +15,14 @@ module OrderManagement
estimate_packages(packages)
end
# Build packages as per stock location
# Build package with default stock location
# No need to check items are in the stock location,
# there is only one stock location so the items will be on that stock location.
#
# It needs to check whether each stock location holds at least one stock
# item for the order. In case none is found it wouldn't make any sense
# to build a package because it would be empty. Plus we avoid errors down
# the stack because it would assume the stock location has stock items
# for the given order
#
# Returns an array of Package instances
def build_packages(packages = [])
Spree::StockLocation.active.each do |stock_location|
next unless stock_location.stock_items.
where(variant_id: order.line_items.pluck(:variant_id)).exists?
packer = build_packer(stock_location, order)
packages += packer.packages
end
packages
# Returns an array with a single Package for the default stock location
def build_packages
packer = build_packer(order)
[packer.package]
end
private
@@ -50,7 +40,8 @@ module OrderManagement
packages
end
def build_packer(stock_location, order)
def build_packer(order)
stock_location = DefaultStockLocation.find_or_create
OrderManagement::Stock::Packer.new(stock_location, order)
end
end

View File

@@ -10,11 +10,7 @@ module OrderManagement
@order = order
end
def packages
[default_package]
end
def default_package
def package
package = OrderManagement::Stock::Package.new(stock_location, order)
order.line_items.each do |line_item|
next unless stock_location.stock_item(line_item.variant)

View File

@@ -10,30 +10,21 @@ module OrderManagement
subject { Packer.new(stock_location, order) }
context 'packages' do
it 'builds an array of packages' do
packages = subject.packages
expect(packages.size).to eq 1
expect(packages.first.contents.size).to eq 5
end
before { order.line_items.first.variant.update(weight: 1) }
it 'builds a package with all the items' do
package = subject.package
expect(package.contents.size).to eq 5
expect(package.weight).to be_positive
end
context 'default_package' do
before { order.line_items.first.variant.update(weight: 1) }
it 'variants are added as backordered without enough on_hand' do
expect(stock_location).to receive(:fill_status).exactly(5).times.and_return([2, 3])
it 'contains all the items' do
package = subject.default_package
expect(package.contents.size).to eq 5
expect(package.weight).to be_positive
end
it 'variants are added as backordered without enough on_hand' do
expect(stock_location).to receive(:fill_status).exactly(5).times.and_return([2, 3])
package = subject.default_package
expect(package.on_hand.size).to eq 5
expect(package.backordered.size).to eq 5
end
package = subject.package
expect(package.on_hand.size).to eq 5
expect(package.backordered.size).to eq 5
end
end
end