Skip to content

Commit 9467289

Browse files
authored
3430 add item quantities to storage location export (#3516)
1 parent 9480a2b commit 9467289

File tree

4 files changed

+66
-3
lines changed

4 files changed

+66
-3
lines changed

app/controllers/storage_locations_controller.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,20 @@ def index
1818
@storage_locations = @storage_locations.kept
1919
end
2020

21+
active_inventory_item_names = []
22+
@storage_locations.each do |storage_location|
23+
active_inventory_item_names <<
24+
storage_location
25+
.active_inventory_items
26+
.joins(:item)
27+
.select('distinct items.name')
28+
.pluck(:name)
29+
end
30+
active_inventory_item_names = active_inventory_item_names.flatten.uniq.sort
31+
2132
respond_to do |format|
2233
format.html
23-
format.csv { send_data StorageLocation.generate_csv(@storage_locations), filename: "StorageLocations-#{Time.zone.today}.csv" }
34+
format.csv { send_data StorageLocation.generate_csv(@storage_locations, active_inventory_item_names), filename: "StorageLocations-#{Time.zone.today}.csv" }
2435
end
2536
end
2637

app/models/storage_location.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ def size
8181
inventory_items.sum(:quantity)
8282
end
8383

84+
def total_active_inventory_count
85+
active_inventory_items
86+
.select('items.quantity')
87+
.sum(:quantity)
88+
end
89+
8490
def inventory_total_value_in_dollars
8591
inventory_total_value = inventory_items.joins(:item).map do |inventory_item|
8692
value_in_cents = inventory_item.item.try(:value_in_cents)
@@ -211,10 +217,18 @@ def self.csv_export_headers
211217
end
212218

213219
def csv_export_attributes
214-
[name, address, square_footage, warehouse_type, size]
220+
attributes = [name, address, square_footage, warehouse_type, total_active_inventory_count]
221+
active_inventory_items.sort_by { |inv_item| inv_item.item.name }.each { |item| attributes << item.quantity }
222+
attributes
215223
end
216224

217225
def empty_inventory_items?
218226
inventory_items.map(&:quantity).uniq.reject(&:zero?).empty?
219227
end
228+
229+
def active_inventory_items
230+
inventory_items
231+
.includes(:item)
232+
.where(items: { active: true })
233+
end
220234
end

spec/models/storage_location_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,5 +228,28 @@
228228
expect(storage_location.longitude).not_to eq(nil)
229229
end
230230
end
231+
232+
describe "csv_export_attributes" do
233+
it "returns an array of storage location attributes, followed by inventory item quantities that are sorted by alphabetized item names" do
234+
item1 = create(:item, name: "C")
235+
item2 = create(:item, name: "B")
236+
item3 = create(:item, name: "A")
237+
inactive_item = create(:item, name: "inactive item", active: false)
238+
name = "New Storage Location"
239+
address = "1500 Remount Road, Front Royal, VA 22630"
240+
warehouse_type = "Warehouse with loading bay"
241+
square_footage = rand(1000..10000)
242+
storage_location = create(:storage_location, name: name, address: address, warehouse_type: warehouse_type, square_footage: square_footage)
243+
quantity1 = rand(100..1000)
244+
quantity2 = rand(100..1000)
245+
quantity3 = rand(100..1000)
246+
create(:inventory_item, storage_location_id: storage_location.id, item_id: item1.id, quantity: quantity1)
247+
create(:inventory_item, storage_location_id: storage_location.id, item_id: item2.id, quantity: quantity2)
248+
create(:inventory_item, storage_location_id: storage_location.id, item_id: item3.id, quantity: quantity3)
249+
create(:inventory_item, storage_location_id: storage_location.id, item_id: inactive_item.id, quantity: 1)
250+
sum = quantity1 + quantity2 + quantity3
251+
expect(storage_location.csv_export_attributes).to eq([name, address, square_footage, warehouse_type, sum, quantity3, quantity2, quantity1])
252+
end
253+
end
231254
end
232255
end

spec/requests/storage_locations_requests_spec.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,26 @@
3838

3939
context "csv" do
4040
let(:response_format) { 'csv' }
41-
4241
it "succeeds" do
4342
get storage_locations_path(default_params.merge(format: response_format))
4443
expect(response).to be_successful
4544
end
45+
46+
it "includes headers followed by alphabetized item names" do
47+
storage_location_with_items = create(:storage_location)
48+
item1 = create(:item, name: 'C')
49+
item2 = create(:item, name: 'B')
50+
item3 = create(:item, name: 'A')
51+
create(:item, name: 'inactive item', active: false)
52+
Item.last(4).each { |item| create(:inventory_item, storage_location_id: storage_location_with_items.id, item_id: item.id, quantity: 1) }
53+
54+
storage_location_with_duplicate_item = create(:storage_location)
55+
create(:inventory_item, storage_location_id: storage_location_with_duplicate_item.id, item_id: item3.id, quantity: 1)
56+
57+
get storage_locations_path(default_params.merge(format: response_format))
58+
59+
expect(response.body.split("\n")[0]).to eq([StorageLocation.csv_export_headers, item3.name, item2.name, item1.name].join(','))
60+
end
4661
end
4762
end
4863

0 commit comments

Comments
 (0)