diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2014-11-14 16:14:03 +0100 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2014-11-14 16:16:22 +0100 |
commit | 057bed03018c3235fbcd3c21abd86f02f65f96c1 (patch) | |
tree | f6b77a9e86fb7e920f884f9f4de595c3839d9415 /lib/reports/sales_by_shop.rb | |
parent | c4bc044881c05822a66d4f77a4ffad9368235731 (diff) | |
download | imusician-reports-057bed03018c3235fbcd3c21abd86f02f65f96c1.tar.gz imusician-reports-057bed03018c3235fbcd3c21abd86f02f65f96c1.tar.bz2 imusician-reports-057bed03018c3235fbcd3c21abd86f02f65f96c1.zip |
Reports pick up their data from the row rather than the sale object.
Diffstat (limited to 'lib/reports/sales_by_shop.rb')
-rw-r--r-- | lib/reports/sales_by_shop.rb | 23 |
1 files changed, 11 insertions, 12 deletions
diff --git a/lib/reports/sales_by_shop.rb b/lib/reports/sales_by_shop.rb index 66caf63..2c8a70b 100644 --- a/lib/reports/sales_by_shop.rb +++ b/lib/reports/sales_by_shop.rb @@ -1,20 +1,19 @@ module SalesReporter module Reports class SalesByShop - def render(sales) - sales_by_shop = {} + def initialize + @shops = Hash.new { |h,k| h[k] = { :count => 0, :total => 0, :sales => [] } } + end - sales.each do |s| - shop = s.shop - if sales_by_shop[shop] - sales_by_shop[shop][:count] += s.quantity - sales_by_shop[shop][:total] += s.amount - else - sales_by_shop[shop] = {:count => s.quantity, :total => s.amount} - end - end + def add_sale(sale, row) + shop = row['Shop'] + @shops[shop][:sales] << sale + @shops[shop][:count] += sale.quantity + @shops[shop][:total] += sale.amount + end - sorted = sales_by_shop.sort { |a,b| b[1][:count] <=> a[1][:count] } + def render + sorted = @shops.sort { |a,b| b[1][:count] <=> a[1][:count] } puts "Shop: Streams: Revenue:" puts "------------------------------------------" |