mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Move adjuster, estimator, packer and prioritizer to order management engine
This commit is contained in:
@@ -107,7 +107,7 @@ module Spree
|
||||
def refresh_rates
|
||||
return shipping_rates if shipped?
|
||||
|
||||
self.shipping_rates = Stock::Estimator.new(order).shipping_rates(to_package)
|
||||
self.shipping_rates = OrderManagement::Stock::Estimator.new(order).shipping_rates(to_package)
|
||||
|
||||
if shipping_method
|
||||
selected_rate = shipping_rates.detect { |rate|
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
# Used by Prioritizer to adjust item quantities
|
||||
# see prioritizer_spec for use cases
|
||||
module Spree
|
||||
module OrderManagement
|
||||
module Stock
|
||||
class Adjuster
|
||||
attr_accessor :variant, :need, :status
|
||||
@@ -38,12 +38,12 @@ module OrderManagement
|
||||
private
|
||||
|
||||
def prioritize_packages(packages)
|
||||
prioritizer = Spree::Stock::Prioritizer.new(order, packages)
|
||||
prioritizer = OrderManagement::Stock::Prioritizer.new(order, packages)
|
||||
prioritizer.prioritized_packages
|
||||
end
|
||||
|
||||
def estimate_packages(packages)
|
||||
estimator = Spree::Stock::Estimator.new(order)
|
||||
estimator = OrderManagement::Stock::Estimator.new(order)
|
||||
packages.each do |package|
|
||||
package.shipping_rates = estimator.shipping_rates(package)
|
||||
end
|
||||
@@ -51,7 +51,7 @@ module OrderManagement
|
||||
end
|
||||
|
||||
def build_packer(stock_location, order)
|
||||
Spree::Stock::Packer.new(stock_location, order)
|
||||
OrderManagement::Stock::Packer.new(stock_location, order)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Spree
|
||||
module OrderManagement
|
||||
module Stock
|
||||
class Estimator
|
||||
attr_reader :order, :currency
|
||||
@@ -1,6 +1,6 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Spree
|
||||
module OrderManagement
|
||||
module Stock
|
||||
class Packer
|
||||
attr_reader :stock_location, :order
|
||||
@@ -17,7 +17,7 @@ module Spree
|
||||
def default_package
|
||||
package = OrderManagement::Stock::Package.new(stock_location, order)
|
||||
order.line_items.each do |line_item|
|
||||
if Config.track_inventory_levels
|
||||
if Spree::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)
|
||||
@@ -1,11 +1,11 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Spree
|
||||
module OrderManagement
|
||||
module Stock
|
||||
class Prioritizer
|
||||
attr_reader :packages, :order
|
||||
|
||||
def initialize(order, packages, adjuster_class = Adjuster)
|
||||
def initialize(order, packages, adjuster_class = OrderManagement::Stock::Adjuster)
|
||||
@order = order
|
||||
@packages = packages
|
||||
@adjuster_class = adjuster_class
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
module Spree
|
||||
module OrderManagement
|
||||
module Stock
|
||||
describe Estimator do
|
||||
let!(:shipping_method) { create(:shipping_method, zones: [Spree::Zone.global] ) }
|
||||
@@ -13,11 +13,11 @@ module Spree
|
||||
context "#shipping rates" do
|
||||
before(:each) do
|
||||
shipping_method.zones.first.members.create(zoneable: order.ship_address.country)
|
||||
allow_any_instance_of(ShippingMethod).to receive_message_chain(:calculator, :available?).and_return(true)
|
||||
allow_any_instance_of(ShippingMethod).to receive_message_chain(:calculator, :compute).and_return(4.00)
|
||||
allow_any_instance_of(ShippingMethod).
|
||||
allow_any_instance_of(Spree::ShippingMethod).to receive_message_chain(:calculator, :available?).and_return(true)
|
||||
allow_any_instance_of(Spree::ShippingMethod).to receive_message_chain(:calculator, :compute).and_return(4.00)
|
||||
allow_any_instance_of(Spree::ShippingMethod).
|
||||
to receive_message_chain(:calculator, :preferences).and_return({ currency: order.currency })
|
||||
allow_any_instance_of(ShippingMethod).to receive_message_chain(:calculator, :marked_for_destruction?)
|
||||
allow_any_instance_of(Spree::ShippingMethod).to receive_message_chain(:calculator, :marked_for_destruction?)
|
||||
|
||||
allow(package).to receive_messages(shipping_methods: [shipping_method])
|
||||
end
|
||||
@@ -39,7 +39,7 @@ module Spree
|
||||
|
||||
context "the calculator is not available for that order" do
|
||||
it "does not return shipping rates from a shipping method" do
|
||||
allow_any_instance_of(ShippingMethod).to receive_message_chain(:calculator, :available?).and_return(false)
|
||||
allow_any_instance_of(Spree::ShippingMethod).to receive_message_chain(:calculator, :available?).and_return(false)
|
||||
shipping_rates = subject.shipping_rates(package)
|
||||
expect(shipping_rates).to eq []
|
||||
end
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
module Spree
|
||||
module OrderManagement
|
||||
module Stock
|
||||
describe Packer do
|
||||
let(:order) { create(:order_with_line_items, line_items_count: 5) }
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
require 'spec_helper'
|
||||
|
||||
module Spree
|
||||
module OrderManagement
|
||||
module Stock
|
||||
describe Prioritizer do
|
||||
let(:order) { create(:order_with_line_items, line_items_count: 2) }
|
||||
@@ -125,7 +125,7 @@ describe Spree::Shipment do
|
||||
let(:mock_estimator) { double('estimator', shipping_rates: shipping_rates) }
|
||||
|
||||
it 'should request new rates, and maintain shipping_method selection' do
|
||||
Spree::Stock::Estimator.should_receive(:new).with(shipment.order).and_return(mock_estimator)
|
||||
OrderManagement::Stock::Estimator.should_receive(:new).with(shipment.order).and_return(mock_estimator)
|
||||
shipment.stub(shipping_method: shipping_method2)
|
||||
|
||||
expect(shipment.refresh_rates).to eq shipping_rates
|
||||
@@ -133,14 +133,14 @@ describe Spree::Shipment do
|
||||
end
|
||||
|
||||
it 'should handle no shipping_method selection' do
|
||||
Spree::Stock::Estimator.should_receive(:new).with(shipment.order).and_return(mock_estimator)
|
||||
OrderManagement::Stock::Estimator.should_receive(:new).with(shipment.order).and_return(mock_estimator)
|
||||
shipment.stub(shipping_method: nil)
|
||||
expect(shipment.refresh_rates).to eq shipping_rates
|
||||
expect(shipment.reload.selected_shipping_rate).to_not be_nil
|
||||
end
|
||||
|
||||
it 'should not refresh if shipment is shipped' do
|
||||
Spree::Stock::Estimator.should_not_receive(:new)
|
||||
OrderManagement::Stock::Estimator.should_not_receive(:new)
|
||||
shipment.shipping_rates.delete_all
|
||||
shipment.stub(shipped?: true)
|
||||
expect(shipment.refresh_rates).to eq []
|
||||
|
||||
Reference in New Issue
Block a user