diff --git a/app/controllers/backoffice/dossiers_controller.rb b/app/controllers/backoffice/dossiers_controller.rb index 7edc11989..2d226c0f3 100644 --- a/app/controllers/backoffice/dossiers_controller.rb +++ b/app/controllers/backoffice/dossiers_controller.rb @@ -39,16 +39,13 @@ class Backoffice::DossiersController < Backoffice::DossiersListController end def download_dossiers_tps - if procedure = Procedure.find_by(id: params[:procedure_id]) - dossiers = dossiers_list_facade(param_liste).dossiers_to_display - respond_with Dossier.export_full_generation(dossiers, request.format) unless dossiers.empty? - else - dossiers = dossiers_list_facade(param_liste).dossiers_to_display - respond_to do |format| - format.xlsx { render xlsx: dossiers } - format.ods { render ods: dossiers } - format.csv { render csv: dossiers } - end + procedure = Procedure.find_by(id: params[:procedure_id]) + export = Dossier.export_full_generation(procedure.dossiers.downloadable) + + respond_to do |format| + format.csv { send_data(SpreadsheetArchitect.to_csv(data: export[:data], headers: export[:headers]), filename: 'dossiers.csv') } + format.xlsx { send_data(SpreadsheetArchitect.to_xlsx(data: export[:data], headers: export[:headers]), filename: 'dossiers.xlsx') } + format.ods { send_data(SpreadsheetArchitect.to_ods(data: export[:data], headers: export[:headers]), filename: 'dossiers.ods') } end end diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 7002b2ab9..67009ed79 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -201,6 +201,8 @@ class Dossier < ActiveRecord::Base scope :archived, -> { where(archived: true) } + scope :downloadable, -> { all_state } + def cerfa_available? procedure.cerfa_flag? && cerfa.size != 0 end @@ -261,21 +263,21 @@ class Dossier < ActiveRecord::Base return headers end - def self.export_full_generation(dossiers, format) - if dossiers && !dossiers.empty? - data = [] + def self.export_full_generation(dossiers) + data = [] + headers = [] + + if dossiers && dossiers.any? headers = dossiers.first.export_headers dossiers.each do |dossier| data << dossier.convert_specific_array_values_to_string(dossier.data_with_champs) end - if ["csv"].include?(format) - return SpreadsheetArchitect.to_csv(data: data, headers: headers) - elsif ["xlsx"].include?(format) - return SpreadsheetArchitect.to_xlsx(data: data, headers: headers) - elsif ["ods"].include?(format) - return SpreadsheetArchitect.to_ods(data: data, headers: headers) - end end + + return { + data: data, + headers: headers + } end def followers_gestionnaires_emails diff --git a/app/views/dossiers/_download_dossiers.html.haml b/app/views/dossiers/_download_dossiers.html.haml index cfd478d8c..71a010a84 100644 --- a/app/views/dossiers/_download_dossiers.html.haml +++ b/app/views/dossiers/_download_dossiers.html.haml @@ -1,13 +1,8 @@ -%div.dropdown.pull-right#download-menu - - if @facade_data_view.dossiers_to_display.count > 400 - %button.btn.btn-error.dropdown-toggle#dropdownDownloadMenu{ type: :button, 'data-toggle' => 'dropdown', 'aria-haspopup' => true, 'aria-expanded' => false, class: 'disabled'} - %span{'data-toggle' => :tooltip, "data-placement" => :left, title: 'Pour réduire le nombre de dossiers et ne pas dépasser la limite autorisée de 400, merci de bien vouloir appliquer des filtres.'} - = t('dynamics.backoffice.limit_excess_download_all_dossiers') - - else - %a.dropdown-toggle#dropdownDownloadMenu.button_navbar{ type: :button, 'data-toggle' => 'dropdown', 'aria-haspopup' => true, 'aria-expanded' => false } - %i.fa.fa-download - = t('dynamics.backoffice.download_all_dossiers') - %span.caret +.dropdown.pull-right#download-menu + %a.dropdown-toggle.button_navbar{ type: :button, 'data-toggle' => 'dropdown', 'aria-haspopup' => true, 'aria-expanded' => false } + %i.fa.fa-download + Télécharger tous les dossiers + %span.caret %ul.dropdown-menu.dropdown-menu-right %li = link_to backoffice_download_dossiers_tps_path(format: :csv, procedure_id: params[:id]), { class: 'btn btn-sm export-link' } do diff --git a/config/locales/dynamics/fr.yml b/config/locales/dynamics/fr.yml index 143e5ca14..72c3aa0f2 100644 --- a/config/locales/dynamics/fr.yml +++ b/config/locales/dynamics/fr.yml @@ -12,8 +12,6 @@ fr: pref_list: title: 'Gestion de colonnes affichées' description: 'Ce menu vous permet de choisir les différentes colonnes que vous souhaitez voir apparaître dans votre interface de suivi des dossiers.' - download_all_dossiers: 'Télécharger mes dossiers' - limit_excess_download_all_dossiers: 'Limite de dossiers fixée à 400 pour le téléchargement' format_csv: 'Au format CSV' format_xlsx: 'Au format XLSX' format_ods: 'Au format ODS' diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 182b8c60b..e688656bd 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -611,7 +611,6 @@ describe Dossier do end describe '#data_with_champs' do - subject { dossier.data_with_champs } it { expect(subject[0]).to be_a_kind_of(Integer) } @@ -626,6 +625,27 @@ describe Dossier do it { expect(subject[9]).to eq(dossier.followers_gestionnaires_emails) } it { expect(subject.count).to eq(DossierProcedureSerializer.new(dossier).attributes.count + dossier.procedure.types_de_champ.count + dossier.export_entreprise_data.count) } end + + describe '.export_full_generation' do + + context 'when there are no dossiers' do + subject { Dossier.export_full_generation(Dossier.none) } + + it { expect(subject[:data]).to eq([]) } + it { expect(subject[:headers]).to eq([]) } + end + + context 'when there are some dossiers' do + let!(:dossier){ create(:dossier) } + let!(:dossier2){ create(:dossier) } + + subject { Dossier.export_full_generation(Dossier.all) } + + it { expect(subject[:data].size).to eq(2) } + it { expect(subject[:headers]).to eq(dossier.export_headers) } + end + + end end describe '#Dossier.to_csv' do @@ -670,7 +690,6 @@ describe Dossier do it { expect(subject[:entreprise_prenom]).to be_nil } end - describe '#Dossier.to_xlsx' do let!(:procedure) { create(:procedure) } let!(:dossier) { create(:dossier, :with_entreprise, user: user, procedure: procedure) } @@ -966,4 +985,17 @@ describe Dossier do end end + + describe '.downloadable' do + let(:procedure) { create(:procedure) } + let!(:dossier) { create(:dossier, :with_entreprise, procedure: procedure, state: :draft) } + let!(:dossier2) { create(:dossier, :with_entreprise, procedure: procedure, state: :initiated) } + let!(:dossier3) { create(:dossier, :with_entreprise, procedure: procedure, state: :received) } + + subject { procedure.dossiers.downloadable } + + it { is_expected.not_to include(dossier)} + it { is_expected.to include(dossier2)} + it { is_expected.to include(dossier3)} + end end