2011-11-22 63 views
1

我卡住合併一些哈希來得到我需要的結果。如何有條件地合併散列並添加值?

散列包含訂單總價格的細目,項目價格,稅收,&送貨,訂購中的所有訂單。我試圖動態執行此操作,因爲並非所有訂單都收取稅費,甚至不需要稅或運費。

這裏就是我稱之爲「最壞的情況」我正在處理:

#First order - has charges for Canadian GST and HST 
{:orderhdr_id=>17654122, :order_item_seq=>1, :order_item_amt_break_seq=>1, :order_item_break_type=>0, :local_amount=>8.16, :base_amount=>8.16, :orig_base_amount=>149, :tax_delivery=>0, :tax_active=>0} 
{:orderhdr_id=>17654122, :order_item_seq=>1, :order_item_amt_break_seq=>2, :order_item_break_type=>1, :local_amount=>0.41, :base_amount=>0.41, :state=>"ON", :tax_type=>"GST", :tax_rate_category=>"STD", :orig_base_amount=>7.45, :tax_rate=>5, :tax_delivery=>0, :tax_active=>1, :tx_incl=>1} 
{:orderhdr_id=>17654122, :order_item_seq=>1, :order_item_amt_break_seq=>3, :order_item_break_type=>1, :local_amount=>0.65, :base_amount=>0.65, :state=>"ON", :tax_type=>"HST", :tax_rate_category=>"STD", :orig_base_amount=>11.92, :tax_rate=>8, :tax_delivery=>0, :tax_active=>1, :tx_incl=>1} 
#Second order - has only one charge for tax 
{:orderhdr_id=>1815296, :order_item_seq=>1, :order_item_amt_break_seq=>1, :order_item_break_type=>0, :local_amount=>76.52, :base_amount=>76.52, :orig_base_amount=>99.95, :tax_delivery=>0, :tax_active=>0} 
{:orderhdr_id=>1815296, :order_item_seq=>1, :order_item_amt_break_seq=>2, :order_item_break_type=>1, :local_amount=>4.59, :base_amount=>4.59, :orig_base_amount=>6, :tax_delivery=>0, :tax_active=>1} 
#Third order - has charge for shipping 
{:orderhdr_id=>6112412, :order_item_seq=>1, :order_item_amt_break_seq=>1, :order_item_break_type=>0, :local_amount=>21.34, :base_amount=>21.34, :orig_base_amount=>99.95, :tax_delivery=>0, :tax_active=>0} 
{:orderhdr_id=>6112412, :order_item_seq=>1, :order_item_amt_break_seq=>2, :order_item_break_type=>2, :local_amount=>4.7, :base_amount=>4.7, :orig_base_amount=>22, :tax_delivery=>0, :tax_active=>0} 

:order_item_break_type決定了它是什麼類型的收費,項目,稅收,航運。

如果:order_item_break_type爲0或2,然後從:orig_base_amount減去:base_amount並將其添加到該中斷類型的整體總。對於:order_item_break_type等於1,我需要總結:orig_base_amount:base_amount,的差異,但是我需要將不同的稅分開。

因此,這裏是我應該與上面的命令結束了一下:

:break_type = 0 | total = 242.88 
:break_type = 1, :state = ON, :tax_type = GST, :tax_rate_category = STD | total = 7.04 
:break_type = 1, :state = ON, :tax_type = HST, :tax_rate_category = STD | total = 11.27 
:break_type = 1 | total = 1.41 
:break_type = 2 | total = 17.30 

我有叫@amounts數組這些哈希值。

我有像merge!,inject,shift和更多的方法,通過我的頭,但不能把它放在一起。

+0

目前尚不清楚結果如何與訂單相關。也就是說,我看到三個訂單和五個結果。你能澄清一下嗎? –

+0

結果是由':break_type',':state',':tax_type'和':tax_rate_category'的唯一組合組合的每次收費的':orig_base_amount - :base_amount'的總和。我傾向於看它的方式是,相對於有3個訂單和5個結果,有7個費用減少到5個結果。謝謝! –

回答

1

這個怎麼樣?不過,我真的不知道你在那裏做什麼。

@amounts = [ 
#First order - has charges for Canadian GST and HST 
{:orderhdr_id=>17654122, :order_item_seq=>1, :order_item_amt_break_seq=>1, :order_item_break_type=>0, :local_amount=>8.16, :base_amount=>8.16, :orig_base_amount=>149, :tax_delivery=>0, :tax_active=>0}, 
{:orderhdr_id=>17654122, :order_item_seq=>1, :order_item_amt_break_seq=>2, :order_item_break_type=>1, :local_amount=>0.41, :base_amount=>0.41, :state=>"ON", :tax_type=>"GST", :tax_rate_category=>"STD", :orig_base_amount=>7.45, :tax_rate=>5, :tax_delivery=>0, :tax_active=>1, :tx_incl=>1}, 
{:orderhdr_id=>17654122, :order_item_seq=>1, :order_item_amt_break_seq=>3, :order_item_break_type=>1, :local_amount=>0.65, :base_amount=>0.65, :state=>"ON", :tax_type=>"HST", :tax_rate_category=>"STD", :orig_base_amount=>11.92, :tax_rate=>8, :tax_delivery=>0, :tax_active=>1, :tx_incl=>1}, 
#Second order - has only one charge for tax 
{:orderhdr_id=>1815296, :order_item_seq=>1, :order_item_amt_break_seq=>1, :order_item_break_type=>0, :local_amount=>76.52, :base_amount=>76.52, :orig_base_amount=>99.95, :tax_delivery=>0, :tax_active=>0}, 
{:orderhdr_id=>1815296, :order_item_seq=>1, :order_item_amt_break_seq=>2, :order_item_break_type=>1, :local_amount=>4.59, :base_amount=>4.59, :orig_base_amount=>6, :tax_delivery=>0, :tax_active=>1}, 
#Third order - has charge for shipping 
{:orderhdr_id=>6112412, :order_item_seq=>1, :order_item_amt_break_seq=>1, :order_item_break_type=>0, :local_amount=>21.34, :base_amount=>21.34, :orig_base_amount=>99.95, :tax_delivery=>0, :tax_active=>0}, 
{:orderhdr_id=>6112412, :order_item_seq=>1, :order_item_amt_break_seq=>2, :order_item_break_type=>2, :local_amount=>4.7, :base_amount=>4.7, :orig_base_amount=>22, :tax_delivery=>0, :tax_active=>0}, 
] 

@totals = Hash.new(0) 

@amounts.group_by{|row| row[:order_item_break_type]}.each do |break_type, rows| 

    rows.each do |row| 

    key = [break_type, row[:tax_type]] 
    @totals[key] += row[:orig_base_amount] - row[:base_amount] 

    end 
end 

@totals 
# => {[0, nil]=>242.88, 
#  [1, "GST"]=>7.04, 
#  [1, "HST"]=>11.27, 
#  [1, nil]=>1.4100000000000001, 
#  [2, nil]=>17.3} 
+0

我認爲這可能有效。我必須通過幾種不同的場景,但乍看起來它看起來不錯。 –

+0

實際上,根本不需要外循環('group_by')。只需使用'inject'或'each_with_object'而不是'each'即可。 –

+0

對不起,我花了很長時間回到這個......瘋狂的時間表。無論如何,我嘗試了它,它確實如我所願,所以,謝謝! –