Skip to content

Commit d5a03a1

Browse files
jeduardo824armahillo
authored andcommitted
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
1 parent ff7fbaa commit d5a03a1

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

app/models/storage_location.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ def size
6060
inventory_items.sum(:quantity)
6161
end
6262

63+
def inventory_total_value_in_dollars
64+
inventory_total_value = inventory_items.joins(:item).map do |inventory_item|
65+
value_in_cents = inventory_item.item.try(:value_in_cents)
66+
value_in_cents * inventory_item.quantity
67+
end.reduce(:+)
68+
inventory_total_value.present? ? (inventory_total_value / 100) : 0
69+
end
70+
6371
def to_csv
6472
org = organization
6573

app/views/storage_locations/_storage_location_row.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<td><%= storage_location.name %></td>
33
<td><%= storage_location.address %></td>
44
<td class="numeric"><%= storage_location.size %></td>
5+
<td><%= number_to_currency(storage_location.inventory_total_value_in_dollars) %></td>
56
<td class="text-right">
67
<%= view_button_to storage_location %>
78
<%= edit_button_to edit_storage_location_path(storage_location) %>

app/views/storage_locations/index.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
<th>Name</th>
5050
<th>Address</th>
5151
<th class="numeric">Total Inventory</th>
52+
<th>Inventory Value</th>
5253
<th class="pull-right">Actions</th>
5354
</tr>
5455
</thead>

spec/models/storage_location_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,22 @@
145145
end
146146
end
147147

148+
describe "inventory_total_value_in_dollars" do
149+
it "returns total value of all items in this storage location" do
150+
storage_location = create(:storage_location)
151+
item1 = create(:item, value_in_cents: 100)
152+
item2 = create(:item, value_in_cents: 200)
153+
create(:inventory_item, storage_location_id: storage_location.id, item_id: item1.id, quantity: 10)
154+
create(:inventory_item, storage_location_id: storage_location.id, item_id: item2.id, quantity: 10)
155+
expect(storage_location.inventory_total_value_in_dollars).to eq(30)
156+
end
157+
158+
it "returns 0 when there are no items in this storage location" do
159+
storage_location = create(:storage_location)
160+
expect(storage_location.inventory_total_value_in_dollars).to eq(0)
161+
end
162+
end
163+
148164
describe "import_csv" do
149165
it "imports storage locations from a csv file" do
150166
before_import = StorageLocation.count

0 commit comments

Comments
 (0)