From 269881db129cfe6ccc888495ac01ceb5f0448b1e Mon Sep 17 00:00:00 2001 From: clemkeirua Date: Thu, 27 Feb 2020 17:39:11 +0100 Subject: [PATCH 1/6] =?UTF-8?q?generation=20d'un=20pdf=20vide=20=C3=A0=20p?= =?UTF-8?q?artir=20d'une=20procedure?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/users/commencer_controller.rb | 16 ++ app/views/dossiers/dossier_vide.pdf.prawn | 189 ++++++++++++++++++ config/routes.rb | 2 + 3 files changed, 207 insertions(+) create mode 100644 app/views/dossiers/dossier_vide.pdf.prawn diff --git a/app/controllers/users/commencer_controller.rb b/app/controllers/users/commencer_controller.rb index a6d4ea649..3196a0df4 100644 --- a/app/controllers/users/commencer_controller.rb +++ b/app/controllers/users/commencer_controller.rb @@ -16,6 +16,22 @@ module Users render 'commencer/show' end + def dossier_vide_pdf + @procedure = retrieve_procedure + return procedure_not_found if @procedure.blank? || @procedure.brouillon? + + @dossier = @procedure.new_dossier + render(file: 'dossiers/dossier_vide', formats: [:pdf]) + end + + def dossier_vide_pdf_test + @procedure = retrieve_procedure + return procedure_not_found if @procedure.blank? || @procedure.publiee? + + @dossier = @procedure.new_dossier + render(file: 'dossiers/dossier_vide', formats: [:pdf]) + end + def sign_in @procedure = retrieve_procedure return procedure_not_found if @procedure.blank? diff --git a/app/views/dossiers/dossier_vide.pdf.prawn b/app/views/dossiers/dossier_vide.pdf.prawn new file mode 100644 index 000000000..7ec97f71d --- /dev/null +++ b/app/views/dossiers/dossier_vide.pdf.prawn @@ -0,0 +1,189 @@ +require 'prawn/measurement_extensions' + +def render_in_2_columns(pdf, label, text) + pdf.text_box label, width: 200, height: 100, overflow: :expand, at: [0, pdf.cursor] + pdf.text_box ":", width: 10, height: 100, overflow: :expand, at: [100, pdf.cursor] + pdf.text_box text, width: 420, height: 100, overflow: :expand, at: [110, pdf.cursor] + pdf.text "\n" +end + +def format_in_2_lines(pdf, label, nb_lines = 1) + add_single_line(pdf, label, 12, :bold) + + height = 10 * (nb_lines+1) + pdf.bounding_box([0, pdf.cursor],:width => 460,:height => height) do + pdf.stroke_bounds + end + pdf.text "\n" +end + +def format_in_2_columns(pdf, label) + pdf.text_box label, width: 200, height: 100, overflow: :expand, at: [0, pdf.cursor] + pdf.bounding_box([110, pdf.cursor+5],:width => 350,:height => 20) do + pdf.stroke_bounds + end + + pdf.text "\n" +end + +def format_with_checkbox(pdf, label, offset = 0) + pdf.font 'liberation serif', size: 12 do + pdf.stroke_rectangle [0 + offset, pdf.cursor], 10, 10 + pdf.text_box label, at: [15 + offset, pdf.cursor] + end + pdf.text "\n" +end + +def add_page_numbering(pdf) + # This method should be called at the end of the drawing since pages drawn after + # do not have page numbering + string = ' / ' + options = { + at: [0, -15], + align: :right + } + pdf.number_pages string, options +end + +def format_date(date) + I18n.l(date, format: :message_date_with_year) +end + +def add_identite_individual(pdf, dossier) + format_in_2_columns(pdf, "Civilité") + format_in_2_columns(pdf, "Nom") + format_in_2_columns(pdf, "Prénom") + format_in_2_columns(pdf, "Date de naissance") +end + +def add_identite_etablissement(pdf, libelle) + add_single_line(pdf, libelle, 12, :bold) + + format_in_2_columns(pdf, "SIRET") + format_in_2_columns(pdf, "Dénomination") + format_in_2_columns(pdf, "Forme juridique") +end + +def add_single_line(pdf, libelle, size, style) + pdf.font 'liberation serif', style: style, size: size do + pdf.text libelle + end +end + +def add_title(pdf, title) + add_single_line(pdf, title, 24, :bold) + pdf.text "\n" +end + +def add_libelle(pdf, champ) + add_single_line(pdf, champ.libelle, 12, :bold) +end + +def add_explanation(pdf, explanation) + add_single_line(pdf, explanation, 9, :italic) +end + +def render_single_champ(pdf, champ) + case champ.type + when 'Champs::RepetitionChamp' + raise 'There should not be a RepetitionChamp here !' + when 'Champs::PieceJustificativeChamp' + add_single_line(pdf, 'Pièce justificative à joindre', 12, :bold) + pdf.text champ.libelle + pdf.text champ.description + pdf.text "\n" + when 'Champs::YesNoChamp', 'Champs::CheckboxChamp' + add_libelle(pdf, champ) + add_explanation(pdf, 'Cochez la mention applicable') + format_with_checkbox(pdf, 'Oui') + format_with_checkbox(pdf, 'Non') + pdf.text "\n" + when 'Champs::CiviliteChamp' + add_libelle(pdf, champ) + format_with_checkbox(pdf, Individual::GENDER_FEMALE) + format_with_checkbox(pdf, Individual::GENDER_MALE) + pdf.text "\n" + when 'Champs::HeaderSectionChamp' + add_single_line(pdf, champ.libelle, 18, :bold) + pdf.text "\n" + when 'Champs::ExplicationChamp' + add_libelle(pdf, champ) + pdf.text champ.description + pdf.text "\n" + when 'Champs::AddressChamp', 'Champs::CarteChamp', 'Champs::TextareaChamp' + format_in_2_lines(pdf, champ.libelle, 3) + when 'Champs::DropDownListChamp' + add_libelle(pdf, champ) + add_explanation(pdf, 'Cochez la mention applicable, une seule valeur possible') + champ.drop_down_list.options.reject(&:blank?).each do |option| + format_with_checkbox(pdf, option) + end + pdf.text "\n" + when 'Champs::MultipleDropDownListChamp' + add_libelle(pdf, champ) + add_explanation(pdf, 'Cochez la mention applicable, plusieurs valeurs possibles') + champ.drop_down_list.options.reject(&:blank?).each do |option| + format_with_checkbox(pdf, option) + end + pdf.text "\n" + when 'Champs::LinkedDropDownListChamp' + add_libelle(pdf, champ) + champ.primary_options.reject(&:blank?).each do |o| + format_with_checkbox(pdf, o) + champ.secondary_options[o].reject(&:blank?).each do |secondary_option| + format_with_checkbox(pdf, secondary_option, 15) + end + end + pdf.text "\n" + when 'Champs::SiretChamp' + add_identite_etablissement(pdf, champ.libelle) + else + format_in_2_lines(pdf, champ.libelle) + end +end + +def add_champs(pdf, champs) + champs.each do |champ| + if champ.type == 'Champs::RepetitionChamp' + add_libelle(pdf, champ) + (1..3).each do + champ.rows.each do |row| + row.each do |inner_champ| + render_single_champ(pdf, inner_champ) + end + end + end + else + render_single_champ(pdf, champ) + end + end +end + +prawn_document(page_size: "A4") do |pdf| + pdf.font_families.update( 'liberation serif' => { + normal: Rails.root.join('lib/prawn/fonts/liberation_serif/LiberationSerif-Regular.ttf' ), + bold: Rails.root.join('lib/prawn/fonts/liberation_serif/LiberationSerif-Bold.ttf' ), + italic: Rails.root.join('lib/prawn/fonts/liberation_serif/LiberationSerif-Italic.ttf' ), + }) + pdf.font 'liberation serif' + pdf.svg IO.read("app/assets/images/header/logo-ds-wide.svg"), width: 300, position: :center + pdf.move_down(40) + + render_in_2_columns(pdf, 'Démarche', @dossier.procedure.libelle) + render_in_2_columns(pdf, 'Organisme', @dossier.procedure.organisation_name) + pdf.text "\n" + + add_title(pdf, "Identité du demandeur") + + format_in_2_columns(pdf, "Email") + if @dossier.procedure.for_individual? + add_identite_individual(pdf, @dossier) + else + render_identite_etablissement(pdf, @dossier.etablissement) if @dossier.etablissement.present? + end + pdf.text "\n" + + add_title(pdf, 'Formulaire') + add_champs(pdf, @dossier.champs) + add_page_numbering(pdf) +end diff --git a/config/routes.rb b/config/routes.rb index 30d4a3402..f2819b0f9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -242,8 +242,10 @@ Rails.application.routes.draw do scope module: 'users' do namespace :commencer do + get '/test/:path/dossier_vide', action: 'dossier_vide_pdf_test', as: :dossier_vide_test get '/test/:path', action: 'commencer_test', as: :test get '/:path', action: 'commencer' + get '/:path/dossier_vide', action: 'dossier_vide_pdf' get '/:path/sign_in', action: 'sign_in', as: :sign_in get '/:path/sign_up', action: 'sign_up', as: :sign_up get '/:path/france_connect', action: 'france_connect', as: :france_connect From 8c44133fd1cf0b8134c773f4c27df00d8be02edc Mon Sep 17 00:00:00 2001 From: clemkeirua Date: Thu, 5 Mar 2020 09:51:55 +0100 Subject: [PATCH 2/6] ajout de l'intitule des procedures en haut de page --- app/views/dossiers/dossier_vide.pdf.prawn | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/views/dossiers/dossier_vide.pdf.prawn b/app/views/dossiers/dossier_vide.pdf.prawn index 7ec97f71d..fda36b022 100644 --- a/app/views/dossiers/dossier_vide.pdf.prawn +++ b/app/views/dossiers/dossier_vide.pdf.prawn @@ -45,6 +45,12 @@ def add_page_numbering(pdf) pdf.number_pages string, options end +def add_procedure(pdf, dossier) + pdf.repeat(lambda {|page| page > 1 }) do + pdf.draw_text dossier.procedure.libelle, :at => pdf.bounds.top_left + end +end + def format_date(date) I18n.l(date, format: :message_date_with_year) end @@ -186,4 +192,5 @@ prawn_document(page_size: "A4") do |pdf| add_title(pdf, 'Formulaire') add_champs(pdf, @dossier.champs) add_page_numbering(pdf) + add_procedure(pdf, @dossier) end From 90a0879d711d9d40429a9a3f83dac11af8b13fcc Mon Sep 17 00:00:00 2001 From: clemkeirua Date: Thu, 27 Feb 2020 17:39:19 +0100 Subject: [PATCH 3/6] ajout du bouton pour telecharger un pdf vide --- app/views/commencer/show.html.haml | 11 +++++++++++ config/routes.rb | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/app/views/commencer/show.html.haml b/app/views/commencer/show.html.haml index 661b2b4d2..e5374b008 100644 --- a/app/views/commencer/show.html.haml +++ b/app/views/commencer/show.html.haml @@ -40,3 +40,14 @@ %h2.huge-title Vous avez déjà des dossiers pour cette démarche = link_to 'Voir mes dossiers en cours', dossiers_path, class: ['button large expand primary'] = link_to 'Commencer un nouveau dossier', url_for_new_dossier(@procedure), class: ['button large expand'] + + - pdf_link = commencer_dossier_vide_path(path: @procedure.path, format: :pdf) + - if @procedure.brouillon? + - pdf_link = commencer_dossier_vide_test_path(path: @procedure.path, format: :pdf) + %hr + %h2.huge-title Effectuer une demande papier + %p + Vous souhaitez effectuer une demande par papier ? Vous pouvez télécharger un dossier vide au format PDF, + et l'envoyer à l’administration concernée. + %br + = link_to 'Télécharger un dossier vide au format PDF', pdf_link, class: ['button large expand'] diff --git a/config/routes.rb b/config/routes.rb index f2819b0f9..32bc4a620 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -245,7 +245,7 @@ Rails.application.routes.draw do get '/test/:path/dossier_vide', action: 'dossier_vide_pdf_test', as: :dossier_vide_test get '/test/:path', action: 'commencer_test', as: :test get '/:path', action: 'commencer' - get '/:path/dossier_vide', action: 'dossier_vide_pdf' + get '/:path/dossier_vide', action: 'dossier_vide_pdf', as: :dossier_vide get '/:path/sign_in', action: 'sign_in', as: :sign_in get '/:path/sign_up', action: 'sign_up', as: :sign_up get '/:path/france_connect', action: 'france_connect', as: :france_connect From ca7f81b6ede0c650c317125fe27ba815fa9583bd Mon Sep 17 00:00:00 2001 From: clemkeirua Date: Wed, 4 Mar 2020 15:51:33 +0100 Subject: [PATCH 4/6] add a feature-flip for procedure PDF export --- app/helpers/flipper_helper.rb | 4 ++++ app/models/procedure.rb | 4 ++++ app/views/commencer/show.html.haml | 21 +++++++++++---------- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/app/helpers/flipper_helper.rb b/app/helpers/flipper_helper.rb index aa81299c5..8f1cf221a 100644 --- a/app/helpers/flipper_helper.rb +++ b/app/helpers/flipper_helper.rb @@ -2,4 +2,8 @@ module FlipperHelper def feature_enabled?(feature_name) Flipper.enabled?(feature_name, current_user) end + + def feature_enabled_for?(feature_name, item) + Flipper.enabled?(feature_name, item) + end end diff --git a/app/models/procedure.rb b/app/models/procedure.rb index a27e57b47..807899628 100644 --- a/app/models/procedure.rb +++ b/app/models/procedure.rb @@ -496,6 +496,10 @@ class Procedure < ApplicationRecord dossiers.discard_all end + def flipper_id + "Procedure;#{id}" + end + private def move_type_de_champ_attributes(types_de_champ, type_de_champ, new_index) diff --git a/app/views/commencer/show.html.haml b/app/views/commencer/show.html.haml index e5374b008..10b712f28 100644 --- a/app/views/commencer/show.html.haml +++ b/app/views/commencer/show.html.haml @@ -41,13 +41,14 @@ = link_to 'Voir mes dossiers en cours', dossiers_path, class: ['button large expand primary'] = link_to 'Commencer un nouveau dossier', url_for_new_dossier(@procedure), class: ['button large expand'] - - pdf_link = commencer_dossier_vide_path(path: @procedure.path, format: :pdf) - - if @procedure.brouillon? - - pdf_link = commencer_dossier_vide_test_path(path: @procedure.path, format: :pdf) - %hr - %h2.huge-title Effectuer une demande papier - %p - Vous souhaitez effectuer une demande par papier ? Vous pouvez télécharger un dossier vide au format PDF, - et l'envoyer à l’administration concernée. - %br - = link_to 'Télécharger un dossier vide au format PDF', pdf_link, class: ['button large expand'] + - if feature_enabled_for?(:dossier_pdf_vide, @procedure) + - pdf_link = commencer_dossier_vide_path(path: @procedure.path, format: :pdf) + - if @procedure.brouillon? + - pdf_link = commencer_dossier_vide_test_path(path: @procedure.path, format: :pdf) + %hr + %h2.huge-title Effectuer une demande papier + %p + Vous souhaitez effectuer une demande par papier ? Vous pouvez télécharger un dossier vide au format PDF, + et l'envoyer à l’administration concernée. + %br + = link_to 'Télécharger un dossier vide au format PDF', pdf_link, class: ['button large expand'] From 76488ec7585331029e8d45c62abbf10859a93998 Mon Sep 17 00:00:00 2001 From: clemkeirua Date: Wed, 4 Mar 2020 16:30:45 +0100 Subject: [PATCH 5/6] =?UTF-8?q?ajout=20de=20l'adresse=20du=20service=20o?= =?UTF-8?q?=C3=B9=20envoyer=20le=20dossier?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/views/commencer/show.html.haml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/commencer/show.html.haml b/app/views/commencer/show.html.haml index 10b712f28..c9b167e39 100644 --- a/app/views/commencer/show.html.haml +++ b/app/views/commencer/show.html.haml @@ -46,9 +46,9 @@ - if @procedure.brouillon? - pdf_link = commencer_dossier_vide_test_path(path: @procedure.path, format: :pdf) %hr - %h2.huge-title Effectuer une demande papier %p Vous souhaitez effectuer une demande par papier ? Vous pouvez télécharger un dossier vide au format PDF, - et l'envoyer à l’administration concernée. + et l'envoyer à l’administration concernée : + #{@procedure&.service&.nom} - #{@procedure&.service&.adresse} %br = link_to 'Télécharger un dossier vide au format PDF', pdf_link, class: ['button large expand'] From debc6628d83837ee88b46b5165ef90a5f8f30bc4 Mon Sep 17 00:00:00 2001 From: clemkeirua Date: Tue, 17 Mar 2020 14:51:27 +0100 Subject: [PATCH 6/6] =?UTF-8?q?generation=20d'un=20fichier=20pdf=20bien=20?= =?UTF-8?q?nomm=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit La route ne contient pas '.pdf' dans l'url, mais le controller génère un fichier pdf du nom de la procédure et lance le téléchargement immédiatement --- app/controllers/users/commencer_controller.rb | 12 ++++++++---- app/views/commencer/show.html.haml | 4 ++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/app/controllers/users/commencer_controller.rb b/app/controllers/users/commencer_controller.rb index 3196a0df4..a5ddcf5fe 100644 --- a/app/controllers/users/commencer_controller.rb +++ b/app/controllers/users/commencer_controller.rb @@ -20,16 +20,14 @@ module Users @procedure = retrieve_procedure return procedure_not_found if @procedure.blank? || @procedure.brouillon? - @dossier = @procedure.new_dossier - render(file: 'dossiers/dossier_vide', formats: [:pdf]) + generate_empty_pdf(@procedure) end def dossier_vide_pdf_test @procedure = retrieve_procedure return procedure_not_found if @procedure.blank? || @procedure.publiee? - @dossier = @procedure.new_dossier - render(file: 'dossiers/dossier_vide', formats: [:pdf]) + generate_empty_pdf(@procedure) end def sign_in @@ -81,5 +79,11 @@ module Users def store_user_location!(procedure) store_location_for(:user, helpers.procedure_lien(procedure)) end + + def generate_empty_pdf(procedure) + @dossier = procedure.new_dossier + s = render_to_string(file: 'dossiers/dossier_vide', formats: [:pdf]) + send_data(s, :filename => "#{procedure.libelle}.pdf") + end end end diff --git a/app/views/commencer/show.html.haml b/app/views/commencer/show.html.haml index c9b167e39..e37559876 100644 --- a/app/views/commencer/show.html.haml +++ b/app/views/commencer/show.html.haml @@ -42,9 +42,9 @@ = link_to 'Commencer un nouveau dossier', url_for_new_dossier(@procedure), class: ['button large expand'] - if feature_enabled_for?(:dossier_pdf_vide, @procedure) - - pdf_link = commencer_dossier_vide_path(path: @procedure.path, format: :pdf) + - pdf_link = commencer_dossier_vide_path(path: @procedure.path) - if @procedure.brouillon? - - pdf_link = commencer_dossier_vide_test_path(path: @procedure.path, format: :pdf) + - pdf_link = commencer_dossier_vide_test_path(path: @procedure.path) %hr %p Vous souhaitez effectuer une demande par papier ? Vous pouvez télécharger un dossier vide au format PDF,