Skip to content

Commit

Permalink
Resolve #4746: Add total of FMVs to Purchase Index; Fix pagination of…
Browse files Browse the repository at this point in the history
… Amount spent (#4847)

* feat: add total amount spent and FMV with pagination behaviour

* fix: use paginated_purchases to calculate paginated_fair_market_values

* fixing mis-merge

---------

Co-authored-by: CL Fisher <[email protected]>
Co-authored-by: CL Fisher <[email protected]>
  • Loading branch information
3 people authored Jan 12, 2025
1 parent d605dca commit 016e957
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
7 changes: 7 additions & 0 deletions app/controllers/purchases_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
15 changes: 14 additions & 1 deletion app/views/purchases/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,20 @@
<%= @paginated_purchases_quantity %> (This page)
</td>
<td></td>
<td class="numeric"><strong><%= dollar_value(@total_value_all_purchases) %></strong></td>
<td class="numeric">
<strong>
<%= dollar_value(@total_value_all_purchases) %> (Total)
</strong>
<br>
<%= dollar_value(@paginated_purchases_value) %> (This page)
</td>
<td class="numeric">
<strong>
<%= dollar_value(@total_fair_market_values) %> (Total)
</strong>
<br>
<%= dollar_value(@paginated_fair_market_values) %> (This page)
</td>
<td></td>
</tr>
</tfoot>
Expand Down
43 changes: 43 additions & 0 deletions spec/requests/purchases_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 016e957

Please sign in to comment.