diff --git a/app/services/distribution_itemized_breakdown_service.rb b/app/services/distribution_itemized_breakdown_service.rb index 44c4caadf7..60e813cd53 100644 --- a/app/services/distribution_itemized_breakdown_service.rb +++ b/app/services/distribution_itemized_breakdown_service.rb @@ -59,7 +59,7 @@ def distributions end def current_onhand_quantities(inventory) - inventory.all_items.group_by(&:name).to_h { |k, v| [k, v.sum(&:quantity)] } + inventory.all_items.each_with_object(Hash.new(0)) { |i, h| h[i.name] += i.quantity } end def current_onhand_minimums(inventory) diff --git a/app/views/reports/itemized_distributions.html.erb b/app/views/reports/itemized_distributions.html.erb index 2cc88311a6..6c2d2ff5ae 100644 --- a/app/views/reports/itemized_distributions.html.erb +++ b/app/views/reports/itemized_distributions.html.erb @@ -27,7 +27,7 @@ <%= item[:name] %> <%= item[:distributed] %> - <%= item[:current_onhand] || "Unknown" %> + <%= item[:current_onhand] %> <% end %> diff --git a/app/views/reports/itemized_donations.html.erb b/app/views/reports/itemized_donations.html.erb index dfdba32ad0..3ef53a957b 100644 --- a/app/views/reports/itemized_donations.html.erb +++ b/app/views/reports/itemized_donations.html.erb @@ -27,7 +27,7 @@ <%= item[:name] %> <%= item[:donated] %> - <%= item[:current_onhand] || "Unknown" %> + <%= item[:current_onhand] %> <% end %> diff --git a/spec/services/distribution_itemized_breakdown_service_spec.rb b/spec/services/distribution_itemized_breakdown_service_spec.rb index 847a782726..33155172f6 100644 --- a/spec/services/distribution_itemized_breakdown_service_spec.rb +++ b/spec/services/distribution_itemized_breakdown_service_spec.rb @@ -26,6 +26,17 @@ it "should include the break down of items distributed with onhand data" do expect(subject).to eq(expected_output) end + + context "#fetch with inactive item" do + let(:inactive_item) { create(:item, :inactive, organization: organization, name: "Inactive Item") } + let(:distribution) { create(:distribution, :with_items, item: inactive_item, organization: organization) } + let(:service) { described_class.new(organization: organization, distribution_ids: [distribution.id]) } + + it "returns current_onhand as 0 for inactive items" do + result = service.fetch + expect(result.find { |i| i[:name] == inactive_item.name }[:current_onhand]).to eq(0) + end + end end describe "#fetch_csv" do