From 32c1084df20d2a5e5c03b0a597f3feaa579e9726 Mon Sep 17 00:00:00 2001
From: Hungle2911 <122929293+Hungle2911@users.noreply.github.com>
Date: Wed, 11 Dec 2024 16:16:02 -0500
Subject: [PATCH 1/3] feat: add total amount spent and FMV with pagination
behaviour
---
app/controllers/purchases_controller.rb | 7 ++++
app/views/purchases/index.html.erb | 15 ++++++++-
spec/requests/purchases_requests_spec.rb | 43 ++++++++++++++++++++++++
3 files changed, 64 insertions(+), 1 deletion(-)
diff --git a/app/controllers/purchases_controller.rb b/app/controllers/purchases_controller.rb
index b11b8cb2ed..116bebf389 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 = @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 cdbda040b8..8fac705a4e 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 478e36f961..d62a066d9b 100644
--- a/spec/requests/purchases_requests_spec.rb
+++ b/spec/requests/purchases_requests_spec.rb
@@ -36,6 +36,49 @@
expect(subject.body).to include("Comments")
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
end
context "csv" do
From 9ba0387d6afc6509067de04204e439c6f96fb087 Mon Sep 17 00:00:00 2001
From: Hungle2911 <122929293+Hungle2911@users.noreply.github.com>
Date: Fri, 13 Dec 2024 15:25:44 -0500
Subject: [PATCH 2/3] fix: use paginated_purchases to calculate
paginated_fair_market_values
---
app/controllers/purchases_controller.rb | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/app/controllers/purchases_controller.rb b/app/controllers/purchases_controller.rb
index 116bebf389..b1056a6958 100644
--- a/app/controllers/purchases_controller.rb
+++ b/app/controllers/purchases_controller.rb
@@ -21,7 +21,7 @@ def index
@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 = @purchases.collect(&:value_per_itemizable).sum
+ @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]
From 7ce963b649983de9c2650882c17595f4f8d7d450 Mon Sep 17 00:00:00 2001
From: CL Fisher
Date: Sat, 14 Dec 2024 20:11:31 -0500
Subject: [PATCH 3/3] fixing mis-merge
---
spec/requests/purchases_requests_spec.rb | 2 ++
1 file changed, 2 insertions(+)
diff --git a/spec/requests/purchases_requests_spec.rb b/spec/requests/purchases_requests_spec.rb
index 5099f3782d..d535a67e1e 100644
--- a/spec/requests/purchases_requests_spec.rb
+++ b/spec/requests/purchases_requests_spec.rb
@@ -78,6 +78,8 @@
it 'displays correct total fair market values' do
expect(response.body).to include("$30.00")
+ end
+ end
describe "pagination" do
around do |ex|