Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve #4746: Add total of FMVs to Purchase Index; Fix pagination of Amount spent #4847

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = @purchases.collect(&:value_per_itemizable).sum
cielf marked this conversation as resolved.
Show resolved Hide resolved
# 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 @@ -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
Expand Down