Skip to content

Commit

Permalink
Added the ability to define a max weight per package.
Browse files Browse the repository at this point in the history
The max weight per package is useful if you want to

a) limit package weights so they don't go above carrier limits like UPS's 150lb limit.
b) have a company rule that splits boxes after a certain weight.

It is implemented to respect the countries max weight if that happens to be lower.

Signed-off-by: Nathan Lowrie <nate@finelineautomation.com>

Fixes #48
  • Loading branch information
FineLineAutomation authored and radar committed Jan 7, 2013
1 parent 50c487a commit 01b98fd
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
16 changes: 14 additions & 2 deletions app/models/spree/calculator/active_shipping/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def retrieve_timings(origin, destination, packages)
def convert_order_to_weights_array(order)
multiplier = Spree::ActiveShipping::Config[:unit_multiplier]
default_weight = Spree::ActiveShipping::Config[:default_weight]
max_weight = max_weight_for_country(order.ship_address.country)
max_weight = get_max_weight(order)

weights = order.line_items.map do |line_item|
item_weight = line_item.variant.weight.to_f
Expand Down Expand Up @@ -194,7 +194,7 @@ def packages(order)
units = Spree::ActiveShipping::Config[:units].to_sym
packages = []
weights = convert_order_to_weights_array(order)
max_weight = max_weight_for_country(order.ship_address.country)
max_weight = get_max_weight(order)

if max_weight <= 0
packages << Package.new(weights.sum, [], :units => units)
Expand All @@ -214,6 +214,18 @@ def packages(order)
packages
end

def get_max_weight(order)
max_weight = max_weight_for_country(order.ship_address.country)
max_weight_per_package = Spree::ActiveShipping::Config[:max_weight_per_package] * Spree::ActiveShipping::Config[:unit_multiplier]
if max_weight == 0 and max_weight_per_package > 0
max_weight = max_weight_per_package
elsif max_weight > 0 and max_weight_per_package < max_weight and max_weight_per_package > 0
max_weight = max_weight_per_package
end

max_weight
end

def cache_key(order)
addr = order.ship_address
line_items_hash = Digest::MD5.hexdigest(order.line_items.map {|li| li.variant_id.to_s + "_" + li.quantity.to_s }.join("|"))
Expand Down
4 changes: 4 additions & 0 deletions app/views/spree/admin/active_shipping_settings/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@
<%= form.label(:handling_fee, "Handling Fee: ") + tag(:br) %>
<%= preference_field_tag(:handling_fee, @config[:handling_fee], :type => :integer) %>
<% end %>
<%= form.field_container :max_weight_per_package do %>
<%= form.label(:max_weight_per_package, "Max Weight Per Package: ") + tag(:br) %>
<%= preference_field_tag(:max_weight_per_package, @config[:max_weight_per_package], :type => :integer) %>
<% end %>
<%= form.field_container :test_mode do %>
<%= form.label(:test_mode, "Test Mode: ") + tag(:br) %>
<%= preference_field_tag(:test_mode, @config[:test_mode], :type => :boolean) %>
Expand Down
4 changes: 4 additions & 0 deletions app/views/spree/admin/active_shipping_settings/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
<td>Handling Fee:</td>
<td><%= @config[:handling_fee] %></td>
</tr>
<tr>
<td>Max Weight Package:</td>
<td><%= @config[:max_weight_per_package] %></td>
</tr>
<tr>
<td>Test Mode:</td>
<td><%= @config[:test_mode] %></td>
Expand Down
3 changes: 2 additions & 1 deletion lib/spree/active_shipping_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Spree::ActiveShippingConfiguration < Spree::Preferences::Configuration
preference :unit_multiplier, :integer, :default => 16 # 16 oz./lb - assumes variant weights are in lbs
preference :default_weight, :integer, :default => 0 # 16 oz./lb - assumes variant weights are in lbs
preference :handling_fee, :integer

preference :max_weight_per_package, :integer, :default => 0 # 0 means no limit

preference :test_mode, :boolean, :default => false
end
11 changes: 11 additions & 0 deletions spec/models/weight_limits_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ module ActiveShipping
international_calculator.send(:max_weight_for_country, country).should == 66.0*Spree::ActiveShipping::Config[:unit_multiplier] # Canada
domestic_calculator.send(:max_weight_for_country, country).should == 70.0*Spree::ActiveShipping::Config[:unit_multiplier]
end

it "should respect the max weight per package" do
Spree::ActiveShipping::Config.set(:max_weight_per_package => 30)
weights = international_calculator.send :convert_order_to_weights_array, order
weights.should == [20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 21.0, 29.0].map{|x| x*Spree::ActiveShipping::Config[:unit_multiplier]}

packages = international_calculator.send :packages, order
packages.size.should == 12
packages.map{|package| package.weight.amount}.should == [20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 21.0, 29.0].map{|x| x*Spree::ActiveShipping::Config[:unit_multiplier]}
packages.map{|package| package.weight.unit}.uniq.should == [:ounces]
end
end

describe "validation of line item weight" do
Expand Down

0 comments on commit 01b98fd

Please sign in to comment.