Skip to content

Commit

Permalink
Connect shipping rates to shipments earlier in Stock::Coordinator
Browse files Browse the repository at this point in the history
This will give the estimator the information it needs to calculate
taxes correctly in the pending tax work.
  • Loading branch information
jordan-brough committed Mar 9, 2016
1 parent 69d88e9 commit 0684205
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
9 changes: 5 additions & 4 deletions core/app/models/spree/stock/coordinator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ def initialize(order, inventory_units = nil)
end

def shipments
packages.map do |package|
package.to_shipment.tap { |s| s.address = order.ship_address }
end
packages.map(&:shipment)
end

private
Expand All @@ -21,6 +19,9 @@ def packages
packages = build_location_configured_packages
packages = build_packages(packages)
packages = prioritize_packages(packages)
packages.each do |package|
package.shipment = package.to_shipment.tap { |s| s.address = order.ship_address }
end
packages = estimate_packages(packages)
validate_packages(packages)
packages
Expand Down Expand Up @@ -122,7 +123,7 @@ def prioritize_packages(packages)
def estimate_packages(packages)
estimator = Spree::Config.stock.estimator_class.new(order)
packages.each do |package|
package.shipping_rates = estimator.shipping_rates(package)
package.shipment.shipping_rates = estimator.shipping_rates(package)
end
packages
end
Expand Down
5 changes: 4 additions & 1 deletion core/app/models/spree/stock/estimator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ def calculate_shipping_rates(package)
end

if cost
rate = shipping_method.shipping_rates.new(cost: cost)
rate = shipping_method.shipping_rates.new(
cost: cost,
shipment: package.shipment
)
rate.tax_rate = tax_rate if tax_rate
end

Expand Down
4 changes: 1 addition & 3 deletions core/app/models/spree/stock/package.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ module Spree
module Stock
class Package
attr_reader :stock_location, :contents
attr_accessor :shipping_rates
attr_accessor :shipment

# @param stock_location [Spree::StockLocation] the stock location this package originates from
# @param contents [Array<Spree::Stock::ContentItem>] the contents of this package
def initialize(stock_location, contents = [])
@stock_location = stock_location
@contents = contents
@shipping_rates = []
end

# Adds an inventory unit to this package.
Expand Down Expand Up @@ -124,7 +123,6 @@ def to_shipment

Spree::Shipment.new(
stock_location: stock_location,
shipping_rates: shipping_rates,
inventory_units: contents.map(&:inventory_unit)
)
end
Expand Down
2 changes: 1 addition & 1 deletion core/spec/models/spree/stock/package_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ def build_inventory_unit
subject.add build_inventory_unit, :backordered

shipping_method = build(:shipping_method)
subject.shipping_rates = [Spree::ShippingRate.new(shipping_method: shipping_method, cost: 10.00, selected: true)]

shipment = subject.to_shipment
shipment.shipping_rates = [Spree::ShippingRate.new(shipping_method: shipping_method, cost: 10.00, selected: true)]
expect(shipment.stock_location).to eq subject.stock_location
expect(shipment.inventory_units.size).to eq 3

Expand Down

0 comments on commit 0684205

Please sign in to comment.