gpt4 book ai didi

ruby-on-rails - Fedex gem,多个包裹

转载 作者:太空宇宙 更新时间:2023-11-03 16:45:46 25 4
gpt4 key购买 nike

我正在尝试根据使用 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...

最佳答案

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件商品-> 1个大包4件+1个小包剩余1件),我们在package数组中添加对应的条件.

关于ruby-on-rails - Fedex gem,多个包裹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33246129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com