diff --git a/app/controllers/purchases_controller.rb b/app/controllers/purchases_controller.rb index 760c06c1b4..77edf7fa5a 100644 --- a/app/controllers/purchases_controller.rb +++ b/app/controllers/purchases_controller.rb @@ -13,9 +13,16 @@ def index @paginated_purchases = @purchases.page(params[:page]) # Are these going to be inefficient with large datasets? # Using the @purchases allows drilling down instead of always starting with the total dataset + # Purchase quantity @purchases_quantity = @purchases.collect(&:total_quantity).sum @paginated_purchases_quantity = @paginated_purchases.collect(&:total_quantity).sum + # Purchase value @total_value_all_purchases = @purchases.sum(&:amount_spent_in_cents) + @paginated_purchases_value = @paginated_purchases.collect(&:amount_spent_in_cents).sum + # Fair Market Values + @total_fair_market_values = @purchases.sum(&:value_per_itemizable) + @paginated_fair_market_values = @paginated_purchases.collect(&:value_per_itemizable).sum + # Storage and Vendor @storage_locations = current_organization.storage_locations.active_locations @selected_storage_location = filter_params[:at_storage_location] @vendors = current_organization.vendors.sort_by { |vendor| vendor.business_name.downcase } diff --git a/app/views/purchases/index.html.erb b/app/views/purchases/index.html.erb index 42f2d482f6..bfc02a9af7 100644 --- a/app/views/purchases/index.html.erb +++ b/app/views/purchases/index.html.erb @@ -102,7 +102,20 @@ <%= @paginated_purchases_quantity %> (This page) - <%= dollar_value(@total_value_all_purchases) %> + + + <%= dollar_value(@total_value_all_purchases) %> (Total) + +
+ <%= dollar_value(@paginated_purchases_value) %> (This page) + + + + <%= dollar_value(@total_fair_market_values) %> (Total) + +
+ <%= dollar_value(@paginated_fair_market_values) %> (This page) + diff --git a/spec/requests/purchases_requests_spec.rb b/spec/requests/purchases_requests_spec.rb index 3e51348c1e..d535a67e1e 100644 --- a/spec/requests/purchases_requests_spec.rb +++ b/spec/requests/purchases_requests_spec.rb @@ -38,6 +38,49 @@ expect(subject.body).to include("Purchase Comment") end + context "with multiple purchases" do + let!(:storage_location) { create(:storage_location, organization: organization) } + let(:vendor) { create(:vendor, organization: organization) } + let!(:purchase1) do + create(:purchase, + organization: organization, + storage_location: storage_location, + vendor: vendor, + amount_spent_in_cents: 1000, + line_items: [ + build(:line_item, quantity: 10, item: create(:item, organization: organization)) + ]) + end + + let!(:purchase2) do + create(:purchase, + organization: organization, + storage_location: storage_location, + vendor: vendor, + amount_spent_in_cents: 1000, + line_items: [ + build(:line_item, quantity: 20, item: create(:item, organization: organization)) + ]) + end + + before do + allow_any_instance_of(Purchase).to receive(:value_per_itemizable).and_return(1500) + get purchases_path(format: 'html') + end + + it 'displays correct total purchase quantities' do + expect(response.body).to include("30") + end + + it 'displays correct total purchase values' do + expect(response.body).to include("$20.00") + end + + it 'displays correct total fair market values' do + expect(response.body).to include("$30.00") + end + end + describe "pagination" do around do |ex| Kaminari.config.default_per_page = 2