Skip to content

Commit 016e957

Browse files
Hungle2911cielf
andauthored
Resolve #4746: Add total of FMVs to Purchase Index; Fix pagination of 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 <cielf@users.noreply.github.com> Co-authored-by: CL Fisher <clfisher@rogers.com>
1 parent d605dca commit 016e957

3 files changed

Lines changed: 64 additions & 1 deletion

File tree

app/controllers/purchases_controller.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,16 @@ def index
1313
@paginated_purchases = @purchases.page(params[:page])
1414
# Are these going to be inefficient with large datasets?
1515
# Using the @purchases allows drilling down instead of always starting with the total dataset
16+
# Purchase quantity
1617
@purchases_quantity = @purchases.collect(&:total_quantity).sum
1718
@paginated_purchases_quantity = @paginated_purchases.collect(&:total_quantity).sum
19+
# Purchase value
1820
@total_value_all_purchases = @purchases.sum(&:amount_spent_in_cents)
21+
@paginated_purchases_value = @paginated_purchases.collect(&:amount_spent_in_cents).sum
22+
# Fair Market Values
23+
@total_fair_market_values = @purchases.sum(&:value_per_itemizable)
24+
@paginated_fair_market_values = @paginated_purchases.collect(&:value_per_itemizable).sum
25+
# Storage and Vendor
1926
@storage_locations = current_organization.storage_locations.active_locations
2027
@selected_storage_location = filter_params[:at_storage_location]
2128
@vendors = current_organization.vendors.sort_by { |vendor| vendor.business_name.downcase }

app/views/purchases/index.html.erb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,20 @@
102102
<%= @paginated_purchases_quantity %> (This page)
103103
</td>
104104
<td></td>
105-
<td class="numeric"><strong><%= dollar_value(@total_value_all_purchases) %></strong></td>
105+
<td class="numeric">
106+
<strong>
107+
<%= dollar_value(@total_value_all_purchases) %> (Total)
108+
</strong>
109+
<br>
110+
<%= dollar_value(@paginated_purchases_value) %> (This page)
111+
</td>
112+
<td class="numeric">
113+
<strong>
114+
<%= dollar_value(@total_fair_market_values) %> (Total)
115+
</strong>
116+
<br>
117+
<%= dollar_value(@paginated_fair_market_values) %> (This page)
118+
</td>
106119
<td></td>
107120
</tr>
108121
</tfoot>

spec/requests/purchases_requests_spec.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,49 @@
3838
expect(subject.body).to include("Purchase Comment")
3939
end
4040

41+
context "with multiple purchases" do
42+
let!(:storage_location) { create(:storage_location, organization: organization) }
43+
let(:vendor) { create(:vendor, organization: organization) }
44+
let!(:purchase1) do
45+
create(:purchase,
46+
organization: organization,
47+
storage_location: storage_location,
48+
vendor: vendor,
49+
amount_spent_in_cents: 1000,
50+
line_items: [
51+
build(:line_item, quantity: 10, item: create(:item, organization: organization))
52+
])
53+
end
54+
55+
let!(:purchase2) do
56+
create(:purchase,
57+
organization: organization,
58+
storage_location: storage_location,
59+
vendor: vendor,
60+
amount_spent_in_cents: 1000,
61+
line_items: [
62+
build(:line_item, quantity: 20, item: create(:item, organization: organization))
63+
])
64+
end
65+
66+
before do
67+
allow_any_instance_of(Purchase).to receive(:value_per_itemizable).and_return(1500)
68+
get purchases_path(format: 'html')
69+
end
70+
71+
it 'displays correct total purchase quantities' do
72+
expect(response.body).to include("30")
73+
end
74+
75+
it 'displays correct total purchase values' do
76+
expect(response.body).to include("$20.00")
77+
end
78+
79+
it 'displays correct total fair market values' do
80+
expect(response.body).to include("$30.00")
81+
end
82+
end
83+
4184
describe "pagination" do
4285
around do |ex|
4386
Kaminari.config.default_per_page = 2

0 commit comments

Comments
 (0)