Skip to content

Commit c7fbbe5

Browse files
authored
Add new purchase information on summary report view (#4723)
1 parent 8a0e42f commit c7fbbe5

File tree

5 files changed

+38
-6
lines changed

5 files changed

+38
-6
lines changed

app/controllers/reports_controller.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ def manufacturer_donations_summary
1212
end
1313

1414
def purchases_summary
15-
@purchases = current_organization.purchases.during(helpers.selected_range)
16-
@recent_purchases = @purchases.recent.includes(:vendor)
15+
@summary_struct = Purchase.organization_summary_by_dates(current_organization, helpers.selected_range)
1716
end
1817

1918
def product_drives_summary

app/models/purchase.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,20 @@ def remove(item)
8181
line_item&.destroy
8282
end
8383

84+
def self.organization_summary_by_dates(organization, date_range)
85+
purchases = where(organization: organization).during(date_range)
86+
87+
OpenStruct.new(
88+
amount_spent: purchases.sum(:amount_spent_in_cents),
89+
recent_purchases: purchases.recent.includes(:vendor),
90+
period_supplies: purchases.sum(:amount_spent_on_period_supplies_cents),
91+
diapers: purchases.sum(:amount_spent_on_diapers_cents),
92+
adult_incontinence: purchases.sum(:amount_spent_on_adult_incontinence_cents),
93+
other: purchases.sum(:amount_spent_on_other_cents),
94+
total_items: purchases.joins(:line_items).sum(:quantity)
95+
)
96+
end
97+
8498
private
8599

86100
def combine_duplicates

app/views/reports/purchases_summary.html.erb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,21 @@
99
filter_url: reports_purchases_summary_path
1010
) do %>
1111
<%= new_button_to new_purchase_path, {text: "New Purchase"} %>
12-
<h3 class="text-center"><%= dollar_presentation(@purchases.sum(&:amount_spent_in_cents)) %>
12+
<h3 class="text-center"><%= dollar_presentation(@summary_struct.amount_spent) %>
1313
spent
1414
<%= @selected_date_range_label %>
1515
</h3>
16+
<div class="box-body" style="margin: 40px">
17+
<h4> Total spent on diapers: <%= dollar_presentation @summary_struct.diapers %></h4>
18+
<h4> Total spent on period supplies: <%= dollar_presentation @summary_struct.period_supplies %> </h4>
19+
<h4> Total spent on adult incontinence: <%= dollar_presentation @summary_struct.adult_incontinence %> </h4>
20+
<h4> Total spent on other: <%= dollar_presentation @summary_struct.other %> </h4>
21+
</div>
22+
<div style="margin: 40px">
23+
<h4> Total items: <%= @summary_struct.total_items %> </h4>
24+
</div>
1625
<div class="box-body">
1726
<h4>Recent purchases</h4>
18-
<%= render partial: "purchase", collection: @recent_purchases, as: :purchase %>
27+
<%= render partial: "purchase", collection: @summary_struct.recent_purchases, as: :purchase %>
1928
</div>
2029
<% end %>

db/seeds.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,7 @@ def seed_quantity(item_name, organization, storage_location, quantity)
709709
# ----------------------------------------------------------------------------
710710

711711
suppliers = %w(Target Wegmans Walmart Walgreens)
712+
amount_items = %w(period_supplies diapers adult_incontinence other)
712713
comments = [
713714
"Maecenas ante lectus, vestibulum pellentesque arcu sed, eleifend lacinia elit. Cras accumsan varius nisl, a commodo ligula consequat nec. Aliquam tincidunt diam id placerat rutrum.",
714715
"Integer a molestie tortor. Duis pretium urna eget congue porta. Fusce aliquet dolor quis viverra volutpat.",
@@ -726,13 +727,18 @@ def seed_quantity(item_name, organization, storage_location, quantity)
726727
comment: comments.sample,
727728
organization_id: pdx_org.id,
728729
storage_location_id: storage_location.id,
729-
amount_spent_in_cents: rand(200..10_000),
730730
issued_at: purchase_date,
731731
created_at: purchase_date,
732732
updated_at: purchase_date,
733-
vendor_id: vendor.id
733+
vendor_id: vendor.id,
734+
amount_spent_on_period_supplies_cents: rand(0..5_000),
735+
amount_spent_on_diapers_cents: rand(0..5_000),
736+
amount_spent_on_adult_incontinence_cents: rand(0..5_000),
737+
amount_spent_on_other_cents: rand(0..5_000)
734738
)
735739

740+
purchase.amount_spent_in_cents = amount_items.map{|i| purchase.send("amount_spent_on_#{i}_cents")}.sum
741+
736742
rand(1..5).times do
737743
purchase.line_items.push(LineItem.new(quantity: rand(1..1000),
738744
item_id: pdx_org.item_ids.sample))

spec/requests/reports/purchases_summary_requests_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
describe "GET #index" do
1111
it "shows a list of recent purchases" do
1212
get reports_purchases_summary_path
13+
expect(response.body).to include("Total spent on diapers")
14+
expect(response.body).to include("Total spent on adult incontinence")
15+
expect(response.body).to include("Total spent on other")
16+
expect(response.body).to include("Total items")
1317
expect(response.body).to include("Recent purchases")
1418
end
1519
end

0 commit comments

Comments
 (0)