Skip to content

Commit

Permalink
1237 - Show total item value for each location (#1279)
Browse files Browse the repository at this point in the history
* add methods to return the invetory total value

* change the view to show the inventory total value
  • Loading branch information
jeduardo824 authored and armahillo committed Oct 17, 2019
1 parent ff7fbaa commit d5a03a1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/models/storage_location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions app/views/storage_locations/_storage_location_row.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<td><%= storage_location.name %></td>
<td><%= storage_location.address %></td>
<td class="numeric"><%= storage_location.size %></td>
<td><%= number_to_currency(storage_location.inventory_total_value_in_dollars) %></td>
<td class="text-right">
<%= view_button_to storage_location %>
<%= edit_button_to edit_storage_location_path(storage_location) %>
Expand Down
1 change: 1 addition & 0 deletions app/views/storage_locations/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
<th>Name</th>
<th>Address</th>
<th class="numeric">Total Inventory</th>
<th>Inventory Value</th>
<th class="pull-right">Actions</th>
</tr>
</thead>
Expand Down
16 changes: 16 additions & 0 deletions spec/models/storage_location_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d5a03a1

Please sign in to comment.