2015-10-20 45 views
2

我正在嘗試計算運費,具體取決於使用Fedex Gem https://github.com/jazminschroeder/fedex購買的產品的總數量。我得到的速度,但我有不同的包選項,其實3。當數量爲1(小)時爲第一個,當數量爲2(中)時爲第二個,當數量爲3或4(較大)時爲第三個。Fedex gem,倍數套餐

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個小包裝的包裝。我用的模想...

+0

你在這裏有什麼問題? – MrYoshiji

+0

問題是...取決於數量是否超過4 ...如果數量是5,如何選擇使用大包裝和1小包裝 – Jose14

回答

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 - > 1大包的4項+ 1小1的剩餘項),我們添加package陣列中的相應條件。