我正在尝试计算运费,具体取决于使用Fedex gem https://github.com/jazminschroeder/fedex购买的产品总数。我得到了费率,但我有不同的套餐选择,实际上是3。 数量为1(小)时的第一个,数量为2(中)时的第二个,数量为3或4(更大)时的第三个。
def packages_types
packages = []
if @order.quantity >= 4
packages << { :weight => {:units => "LB", :value => @order.case_weight},
:dimensions => {:length => 8, :width => 1, :height => 7, :units => "IN" } }
elsif @order.quantity == 2
packages << { :weight => {:units => "LB", :value => 21},
:dimensions => {:length => 1, :width => 2, :height => 7, :units => "IN" } }
elsif @order.quantity == 1
packages << { :weight => {:units => "LB", :value => 10},
:dimensions => {:length => 1, :width => 2, :height => 2, :units => "IN" } }
end
end
因此,如果客户在数量上订购5。这将是4个(大)和1个小包装的包装。我在想使用mod ...
答案 0 :(得分:0)
def packages_types
packages = []
extra_items_count = @order.quantity % 4
large_packages_needed = (@order.quantity - extra_items_count) / 4
# point A
large_packages_needed.times do
packages << { :weight => { :units => "LB", :value => @order.case_weight },
:dimensions => { :length => 8, :width => 1, :height => 7, :units => "IN" } }
end
# point B
case extra_items_count
when 1
packages << { :weight => { :units => "LB", :value => 10 },
:dimensions => { :length => 1, :width => 2, :height => 2, :units => "IN" } }
when 2
packages << { :weight => { :units => "LB", :value => 21 },
:dimensions => { :length => 1, :width => 2, :height => 7, :units => "IN" } }
when 3, 4
packages << { :weight => { :units => "LB", :value => @order.case_weight },
:dimensions => { :length => 8, :width => 1, :height => 7, :units => "IN" } }
end
return packages
end
A点:对于每组4个项目,需要一个大包(并添加到packages
数组中)。
B点:对于其余项目(例如:您订购了5件商品 - &gt; 1件大件包装4件商品+ 1件小件用于1件剩余商品),我们在{添加相应条件{1}}数组。