Skip to content

Commit f945f13

Browse files
writes csv export logic adapted from StorageLocation
1 parent 83508dc commit f945f13

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

app/controllers/audits_controller.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ def index
77
@selected_location = filter_params[:at_location]
88
@audits = current_organization.audits.includes(:line_items, :storage_location).class_filter(filter_params)
99
@storage_locations = StorageLocation.with_audits_for(current_organization).select(:id, :name)
10+
11+
respond_to do |format|
12+
format.html
13+
format.csv do
14+
inventory = View::Inventory.new(current_organization.id)
15+
send_data Audit.generate_csv_from_inventory(@audits, inventory), filename: "Audits-#{Time.zone.today}.csv"
16+
end
17+
end
1018
end
1119

1220
def show

app/models/audit.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Audit < ApplicationRecord
1919
belongs_to :storage_location
2020
belongs_to :adjustment, optional: true
2121

22+
include Exportable
2223
include Itemizable
2324
include Filterable
2425
scope :at_location, ->(location_id) { where(storage_location_id: location_id) }
@@ -49,6 +50,28 @@ def user_is_organization_admin_of_the_organization
4950
end
5051
end
5152

53+
def self.generate_csv_from_inventory(audits, inventory)
54+
all_items = inventory.all_items.uniq(&:item_id).sort_by(&:name)
55+
additional_headers = all_items.map(&:name).uniq
56+
headers = csv_export_headers + additional_headers
57+
58+
CSV.generate(write_headers: true, headers: headers) do |csv|
59+
audits.map do |audit|
60+
sl = audit.storage_location # preloaded earlier with includes to avoid n+1
61+
total_quantity = inventory.quantity_for(storage_location: sl.id)
62+
63+
row = [audit.updated_at.strftime("%B %d %Y"), audit.status, sl.name, sl.address, sl.square_footage, sl.warehouse_type, total_quantity] +
64+
all_items.map { |i| inventory.quantity_for(storage_location: sl.id, item_id: i.item_id) }
65+
66+
csv << row.map { |attr| normalize_csv_attribute(attr) }
67+
end
68+
end
69+
end
70+
71+
def self.csv_export_headers
72+
["Audit Date", "Audit Status", "Name", "Address", "Square Footage", "Warehouse Type", "Total Inventory"]
73+
end
74+
5275
private
5376

5477
def line_items_unique_by_item_id

app/views/audits/index.html.erb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,13 @@
4646
<%= filter_button %>
4747
<%= clear_filter_button %>
4848

49-
<div class="btn-group pull-right">
49+
<span class="float-right">
50+
<%= download_button_to(audits_path(format: :csv), {text: "Export Audit", size: "md"}) %>
5051
<%= new_button_to new_audit_path, {text: "New Audit"} %>
51-
</div>
52+
</span>
5253
<% end # form %>
5354
</div>
54-
</div>
55+
</div>
5556
<!-- /.card -->
5657
</div>
5758
<!--/.col (left) -->

0 commit comments

Comments
 (0)