diff --git a/Gemfile b/Gemfile index 9ef25d08a..d9933ff3a 100644 --- a/Gemfile +++ b/Gemfile @@ -5,6 +5,7 @@ ruby File.read('.ruby-version').strip gem 'rails', '~> 6.1.1' gem 'rails-i18n', '~> 6.0.0' gem 'rdiscount', '~> 2.2.0.1' +gem 'rubyzip', '~> 2.3.0' gem 'activeadmin', '~> 2.9.0' gem 'has_scope', '~> 0.7.2' gem 'pundit', '~> 2.1.0' diff --git a/Gemfile.lock b/Gemfile.lock index e95d62ae3..e3ce0c1c3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -438,6 +438,7 @@ DEPENDENCIES rspec-rails (~> 4.0.0) rubocop (~> 1.6) rubocop-rails (~> 2.9) + rubyzip (~> 2.3.0) sassc-rails (~> 2.1.2) select2-rails (~> 4.0.13) selenium-webdriver (~> 3.142) diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index 1e03e2958..00aa71ffb 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -672,3 +672,8 @@ label[required]::after{ display: inline; margin: 0 !important; } + +.hr-5 { + margin-top: 5px; + margin-bottom: 5px; +} diff --git a/app/controllers/reports_controller.rb b/app/controllers/reports_controller.rb index b757574e3..01f4f3521 100644 --- a/app/controllers/reports_controller.rb +++ b/app/controllers/reports_controller.rb @@ -1,3 +1,4 @@ +require 'zip' class ReportsController < ApplicationController before_action :authenticate_user! @@ -31,6 +32,18 @@ def transfer_list report_responder('Transfer', current_organization, @transfers) end + def all_list + filename = "#{current_organization.name.gsub(' ', '_')}.zip" + temp_file = Tempfile.new(filename) + Zip::File.open(temp_file.path, Zip::File::CREATE) do |zipfile| + add_csvs_to_zip(%w[Member Transfer Inquiries Offers], zipfile) + end + zip_data = File.read(temp_file.path) + send_data(zip_data, type: 'application/zip', disposition: 'attachment', filename: filename) + temp_file.close + temp_file.unlink + end + private def report_responder(report_class, *args) @@ -45,4 +58,51 @@ def download_report(report_class, *args) report = report_class.constantize.new(*args) send_data report.run, filename: report.name, type: report.mime_type end + + def add_csvs_to_zip(report_classes, zip) + report_classes.each do |report_class| + collection = return_collection(report_class) + report = do_report(report_class, collection) + file = Tempfile.new + file.write(report.run) + file.rewind + zip.add("#{report_class}.csv", file.path) + end + end + + def return_collection(report_class) + case report_class + when 'Member' + current_organization.members.active.includes(:user).order('members.member_uid') + when 'Transfer' + current_organization.all_transfers_with_accounts + when 'Inquiries' + return_collection_posts('Inquiry') + when 'Offers' + return_collection_posts('Offer') + else + [] + end + end + + def return_collection_posts(type) + @post_type = type.constantize + @posts = current_organization.posts.of_active_members.active. + merge(@post_type.all). + includes(:user, :category). + group_by(&:category). + to_a. + sort_by { |category, _| category.try(:name).to_s } + end + + def do_report(report_class, collection) + case report_class + when 'Inquiries' + 'Report::Csv::Post'.constantize.new(current_organization, collection, 'Inquiry'.constantize) + when 'Offers' + 'Report::Csv::Post'.constantize.new(current_organization, collection, 'Offer'.constantize) + else + "Report::Csv::#{report_class}".constantize.new(current_organization, collection) + end + end end diff --git a/app/views/application/menus/_organization_reports_menu.html.erb b/app/views/application/menus/_organization_reports_menu.html.erb index d87571675..d9ceb8fa8 100644 --- a/app/views/application/menus/_organization_reports_menu.html.erb +++ b/app/views/application/menus/_organization_reports_menu.html.erb @@ -29,5 +29,12 @@ <%= Transfer.model_name.human(count: :many) %> <% end %> +