diff --git a/Gemfile b/Gemfile index e210be466..24f8c36c9 100644 --- a/Gemfile +++ b/Gemfile @@ -59,7 +59,6 @@ gem 'openid_connect' gem 'pg' gem 'phonelib' gem 'prawn-rails' # PDF Generation -gem 'prawn-svg' gem 'premailer-rails' gem 'puma' # Use Puma as the app server gem 'pundit' diff --git a/Gemfile.lock b/Gemfile.lock index 4485809aa..0faaa9859 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -459,9 +459,6 @@ GEM prawn prawn-table rails (>= 3.1.0) - prawn-svg (0.31.0) - css_parser (~> 1.6) - prawn (>= 0.11.1, < 3) prawn-table (0.2.2) prawn (>= 1.3.0, < 3.0.0) premailer (1.14.2) @@ -840,7 +837,6 @@ DEPENDENCIES pg phonelib prawn-rails - prawn-svg premailer-rails pry-byebug puma diff --git a/app/assets/images/header/logo-ds-wide.png b/app/assets/images/header/logo-ds-wide.png new file mode 100644 index 000000000..a0ac0f353 Binary files /dev/null and b/app/assets/images/header/logo-ds-wide.png differ diff --git a/app/lib/active_storage/downloadable_file.rb b/app/lib/active_storage/downloadable_file.rb index 3f38007be..ac1cb2a8d 100644 --- a/app/lib/active_storage/downloadable_file.rb +++ b/app/lib/active_storage/downloadable_file.rb @@ -1,8 +1,7 @@ class ActiveStorage::DownloadableFile def self.create_list_from_dossiers(dossiers, for_expert = false) - dossiers - .map { |d| pj_and_path(d.id, PiecesJustificativesService.generate_dossier_export(d)) } + - PiecesJustificativesService.liste_documents(dossiers, for_expert) + PiecesJustificativesService.generate_dossier_export(dossiers) + + PiecesJustificativesService.liste_documents(dossiers, for_expert) end private diff --git a/app/models/dossier.rb b/app/models/dossier.rb index e32aa99db..aa18b21d0 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -485,8 +485,9 @@ class Dossier < ApplicationRecord end def motivation - return nil if !termine? - traitement&.motivation || read_attribute(:motivation) + if termine? + traitement&.motivation || read_attribute(:motivation) + end end def update_search_terms diff --git a/app/services/pieces_justificatives_service.rb b/app/services/pieces_justificatives_service.rb index 0bc94b5ad..72b9e9fc8 100644 --- a/app/services/pieces_justificatives_service.rb +++ b/app/services/pieces_justificatives_service.rb @@ -107,21 +107,44 @@ class PiecesJustificativesService end end - def self.generate_dossier_export(dossier) - pdf = ApplicationController - .render(template: 'dossiers/show', formats: [:pdf], - assigns: { - include_infos_administration: true, - dossier: dossier - }) + def self.generate_dossier_export(dossiers) + return [] if dossiers.empty? - FakeAttachment.new( - file: StringIO.new(pdf), - filename: "export-#{dossier.id}.pdf", - name: 'pdf_export_for_instructeur', - id: dossier.id, - created_at: dossier.updated_at - ) + pdfs = [] + + procedure = dossiers.first.procedure + tdc_by_id = TypeDeChamp + .joins(:revisions) + .where(revisions: { id: procedure.revisions }) + .to_a + .group_by(&:id) + + dossiers + .includes(:champs, :champs_private, :commentaires, :individual, + :traitement, :etablissement, + user: :france_connect_information, avis: :expert) + .find_each do |dossier| + pdf = ApplicationController + .render(template: 'dossiers/show', formats: [:pdf], + assigns: { + include_infos_administration: true, + dossier: dossier, + procedure: procedure, + tdc_by_id: tdc_by_id + }) + + a = FakeAttachment.new( + file: StringIO.new(pdf), + filename: "export-#{dossier.id}.pdf", + name: 'pdf_export_for_instructeur', + id: dossier.id, + created_at: dossier.updated_at + ) + + pdfs << ActiveStorage::DownloadableFile.pj_and_path(dossier.id, a) + end + + pdfs end private diff --git a/app/views/dossiers/dossier_vide.pdf.prawn b/app/views/dossiers/dossier_vide.pdf.prawn index 5678ee1b0..f941d0537 100644 --- a/app/views/dossiers/dossier_vide.pdf.prawn +++ b/app/views/dossiers/dossier_vide.pdf.prawn @@ -181,7 +181,7 @@ prawn_document(page_size: "A4") do |pdf| italic: Rails.root.join('lib/prawn/fonts/marianne/marianne-thin.ttf' ), }) pdf.font 'marianne' - pdf.svg IO.read(DOSSIER_PDF_EXPORT_LOGO_SRC), width: 300, position: :center + pdf.image DOSSIER_PDF_EXPORT_LOGO_SRC, width: 300, position: :center pdf.move_down(40) render_in_2_columns(pdf, 'Démarche', @dossier.procedure.libelle) diff --git a/app/views/dossiers/show.pdf.prawn b/app/views/dossiers/show.pdf.prawn index b3b86eba6..182d60fc0 100644 --- a/app/views/dossiers/show.pdf.prawn +++ b/app/views/dossiers/show.pdf.prawn @@ -130,28 +130,30 @@ def add_identite_etablissement(pdf, etablissement) end def add_single_champ(pdf, champ) + tdc = @tdc_by_id[champ.type_de_champ_id].first + case champ.type when 'Champs::PieceJustificativeChamp', 'Champs::TitreIdentiteChamp' return when 'Champs::HeaderSectionChamp' - add_section_title(pdf, champ.libelle) + add_section_title(pdf, tdc.libelle) when 'Champs::ExplicationChamp' - format_in_2_lines(pdf, champ.libelle, champ.description) + format_in_2_lines(pdf, tdc.libelle, tdc.description) when 'Champs::CarteChamp' - format_in_2_lines(pdf, champ.libelle, champ.to_feature_collection.to_json) + format_in_2_lines(pdf, tdc.libelle, champ.to_feature_collection.to_json) when 'Champs::SiretChamp' pdf.font 'marianne', style: :bold do - pdf.text champ.libelle + pdf.text tdc.libelle end if champ.etablissement.present? add_identite_etablissement(pdf, champ.etablissement) end when 'Champs::NumberChamp' value = champ.to_s.empty? ? 'Non communiqué' : number_with_delimiter(champ.to_s) - format_in_2_lines(pdf, champ.libelle, value) + format_in_2_lines(pdf, tdc.libelle, value) else value = champ.to_s.empty? ? 'Non communiqué' : champ.to_s - format_in_2_lines(pdf, champ.libelle, value) + format_in_2_lines(pdf, tdc.libelle, value) end end @@ -205,6 +207,9 @@ def add_etats_dossier(pdf, dossier) end prawn_document(page_size: "A4") do |pdf| + @procedure ||= @dossier.procedure + @tdc_by_id ||= @dossier.champs.map(&:type_de_champ).group_by(&:id) + pdf.font_families.update( 'marianne' => { normal: Rails.root.join('lib/prawn/fonts/marianne/marianne-regular.ttf' ), bold: Rails.root.join('lib/prawn/fonts/marianne/marianne-bold.ttf' ), @@ -212,12 +217,12 @@ prawn_document(page_size: "A4") do |pdf| pdf.font 'marianne' pdf.pad_bottom(40) do - pdf.svg IO.read(DOSSIER_PDF_EXPORT_LOGO_SRC), width: 300, position: :center + pdf.image DOSSIER_PDF_EXPORT_LOGO_SRC, width: 300, position: :center end format_in_2_columns(pdf, 'Dossier Nº', @dossier.id.to_s) - format_in_2_columns(pdf, 'Démarche', @dossier.procedure.libelle) - format_in_2_columns(pdf, 'Organisme', @dossier.procedure.organisation_name) + format_in_2_columns(pdf, 'Démarche', @procedure.libelle) + format_in_2_columns(pdf, 'Organisme', @procedure.organisation_name) add_etat_dossier(pdf, @dossier) diff --git a/config/env.example.optional b/config/env.example.optional index 51b05c16d..8cee6a933 100644 --- a/config/env.example.optional +++ b/config/env.example.optional @@ -69,7 +69,7 @@ DS_ENV="staging" # PROCEDURE_DEFAULT_LOGO_SRC="republique-francaise-logo.svg" # Instance customization: PDF export logo ---> to be put in "app/assets/images" -# DOSSIER_PDF_EXPORT_LOGO_SRC="app/assets/images/header/logo-ds-wide.svg" +# DOSSIER_PDF_EXPORT_LOGO_SRC="app/assets/images/header/logo-ds-wide.png" # Instance customization: watermark for identity documents # WATERMARK_FILE="" diff --git a/config/initializers/images.rb b/config/initializers/images.rb index c488aa279..161c9fc24 100644 --- a/config/initializers/images.rb +++ b/config/initializers/images.rb @@ -17,4 +17,4 @@ MAILER_FOOTER_LOGO_SRC = ENV.fetch("MAILER_FOOTER_LOGO_SRC", "mailer/instructeur PROCEDURE_DEFAULT_LOGO_SRC = ENV.fetch("PROCEDURE_DEFAULT_LOGO_SRC", "republique-francaise-logo.svg") # Logo in PDF export of a "Dossier" -DOSSIER_PDF_EXPORT_LOGO_SRC = ENV.fetch("DOSSIER_PDF_EXPORT_LOGO_SRC", "app/assets/images/header/logo-ds-wide.svg") +DOSSIER_PDF_EXPORT_LOGO_SRC = ENV.fetch("DOSSIER_PDF_EXPORT_LOGO_SRC", "app/assets/images/header/logo-ds-wide.png") diff --git a/spec/services/pieces_justificatives_service_spec.rb b/spec/services/pieces_justificatives_service_spec.rb index 8d0ca2e56..a768a0c63 100644 --- a/spec/services/pieces_justificatives_service_spec.rb +++ b/spec/services/pieces_justificatives_service_spec.rb @@ -160,7 +160,7 @@ describe PiecesJustificativesService do describe '.generate_dossier_export' do let(:dossier) { create(:dossier) } - subject { PiecesJustificativesService.generate_dossier_export(dossier) } + subject { PiecesJustificativesService.generate_dossier_export(Dossier.where(id: dossier.id)) } it "doesn't update dossier" do expect { subject }.not_to change { dossier.updated_at }