From d5a03a13b11f805c77ca2757119eab286178827e Mon Sep 17 00:00:00 2001 From: Eduardo Alencar Date: Thu, 17 Oct 2019 08:17:19 -0300 Subject: [PATCH] 1237 - Show total item value for each location (#1279) * add methods to return the invetory total value * change the view to show the inventory total value --- app/models/storage_location.rb | 8 ++++++++ .../_storage_location_row.html.erb | 1 + app/views/storage_locations/index.html.erb | 1 + spec/models/storage_location_spec.rb | 16 ++++++++++++++++ 4 files changed, 26 insertions(+) diff --git a/app/models/storage_location.rb b/app/models/storage_location.rb index 6872f8a79b..87d5e359b2 100644 --- a/app/models/storage_location.rb +++ b/app/models/storage_location.rb @@ -60,6 +60,14 @@ def size inventory_items.sum(:quantity) end + def inventory_total_value_in_dollars + inventory_total_value = inventory_items.joins(:item).map do |inventory_item| + value_in_cents = inventory_item.item.try(:value_in_cents) + value_in_cents * inventory_item.quantity + end.reduce(:+) + inventory_total_value.present? ? (inventory_total_value / 100) : 0 + end + def to_csv org = organization diff --git a/app/views/storage_locations/_storage_location_row.html.erb b/app/views/storage_locations/_storage_location_row.html.erb index 711d4b3446..c5dec315c8 100644 --- a/app/views/storage_locations/_storage_location_row.html.erb +++ b/app/views/storage_locations/_storage_location_row.html.erb @@ -2,6 +2,7 @@ <%= storage_location.name %> <%= storage_location.address %> <%= storage_location.size %> + <%= number_to_currency(storage_location.inventory_total_value_in_dollars) %> <%= view_button_to storage_location %> <%= edit_button_to edit_storage_location_path(storage_location) %> diff --git a/app/views/storage_locations/index.html.erb b/app/views/storage_locations/index.html.erb index bb49859f16..f41d2f0fc0 100644 --- a/app/views/storage_locations/index.html.erb +++ b/app/views/storage_locations/index.html.erb @@ -49,6 +49,7 @@ Name Address Total Inventory + Inventory Value Actions diff --git a/spec/models/storage_location_spec.rb b/spec/models/storage_location_spec.rb index 92ec566f4f..f17cfc08ae 100644 --- a/spec/models/storage_location_spec.rb +++ b/spec/models/storage_location_spec.rb @@ -145,6 +145,22 @@ end end + describe "inventory_total_value_in_dollars" do + it "returns total value of all items in this storage location" do + storage_location = create(:storage_location) + item1 = create(:item, value_in_cents: 100) + item2 = create(:item, value_in_cents: 200) + create(:inventory_item, storage_location_id: storage_location.id, item_id: item1.id, quantity: 10) + create(:inventory_item, storage_location_id: storage_location.id, item_id: item2.id, quantity: 10) + expect(storage_location.inventory_total_value_in_dollars).to eq(30) + end + + it "returns 0 when there are no items in this storage location" do + storage_location = create(:storage_location) + expect(storage_location.inventory_total_value_in_dollars).to eq(0) + end + end + describe "import_csv" do it "imports storage locations from a csv file" do before_import = StorageLocation.count