Permalink
Browse files

Added the ability to define a max weight per package.

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...
1 parent 17f0e56 commit 5d82b7729ad0044b7f15ca2f4c7f05aa7aaf1318 @FineLineAutomation FineLineAutomation committed with radar Jan 7, 2013
@@ -155,8 +155,8 @@ 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
item_weight = default_weight if item_weight <= 0
@@ -192,8 +192,13 @@ def packages(order)
units = Spree::ActiveShipping::Config[:units].to_sym
packages = []
weights = convert_order_to_weights_array(order)
+<<<<<<< HEAD
max_weight = max_weight_for_country(order.ship_address.country)
+=======
+ max_weight = get_max_weight(order)
+
+>>>>>>> 01b98fd... Added the ability to define a max weight per package.
if max_weight <= 0
packages << Package.new(weights.sum, [], :units => units)
else
@@ -212,6 +217,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("|"))
@@ -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) %>
@@ -39,6 +39,10 @@
<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>
</tr>
@@ -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
@@ -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

0 comments on commit 5d82b77

Please sign in to comment.