diff --git a/app/models/dossier.rb b/app/models/dossier.rb index fa11b0177..16c16618c 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -1295,6 +1295,18 @@ class Dossier < ApplicationRecord sections_for(champ)&.none?(&:libelle_with_section_index?) end + def index_for_section_header(champ) + champs = champ.private? ? champs_private : champs_public + + index = 1 + champs.each do |c| + return index if c.stable_id == champ.stable_id + next unless c.visible? + + index += 1 if c.type_de_champ.header_section? + end + end + private def create_missing_traitemets diff --git a/app/views/dossiers/show.pdf.prawn b/app/views/dossiers/show.pdf.prawn index 741aa31b7..c9a517305 100644 --- a/app/views/dossiers/show.pdf.prawn +++ b/app/views/dossiers/show.pdf.prawn @@ -141,7 +141,13 @@ def add_single_champ(pdf, champ) when 'Champs::PieceJustificativeChamp', 'Champs::TitreIdentiteChamp' return when 'Champs::HeaderSectionChamp' - add_section_title(pdf, tdc.libelle) + libelle = if @dossier.auto_numbering_section_headers_for?(champ) + "#{@dossier.index_for_section_header(champ)}. #{champ.libelle}" + else + champ.libelle + end + + add_section_title(pdf, libelle) when 'Champs::ExplicationChamp' format_in_2_lines(pdf, tdc.libelle, strip_tags(tdc.description)) when 'Champs::CarteChamp' diff --git a/spec/models/dossier_spec.rb b/spec/models/dossier_spec.rb index 354fa0aa4..799f03a6f 100644 --- a/spec/models/dossier_spec.rb +++ b/spec/models/dossier_spec.rb @@ -2116,6 +2116,47 @@ describe Dossier do end end + describe '#index_for_section_header' do + include Logic + let(:number_stable_id) { 99 } + let(:types_de_champ) { + [ + { type: :header_section, libelle: "Infos" }, { type: :integer_number, stable_id: number_stable_id }, + { type: :header_section, libelle: "Details", condition: ds_eq(champ_value(99), constant(5)) }, { type: :header_section, libelle: "Conclusion" } + ] +} + + let(:procedure) { create(:procedure, :for_individual, types_de_champ_public: types_de_champ) } + let(:dossier) { create(:dossier, procedure: procedure) } + + let(:headers) { dossier.champs_public.select { _1.header_section? } } + + let(:number_value) { nil } + + before do + dossier.champs_public.find { _1.stable_id == number_stable_id }.update(value: number_value) + dossier.reload + end + + context "when there are invisible sections" do + it "index accordingly header sections" do + expect(dossier.index_for_section_header(headers[0])).to eq(1) + expect(headers[1]).not_to be_visible + expect(dossier.index_for_section_header(headers[2])).to eq(2) + end + end + + context "when all headers are visible" do + let(:number_value) { 5 } + it "index accordingly header sections" do + expect(dossier.index_for_section_header(headers[0])).to eq(1) + expect(headers[1]).to be_visible + expect(dossier.index_for_section_header(headers[1])).to eq(2) + expect(dossier.index_for_section_header(headers[2])).to eq(3) + end + end + end + private def count_for_month(processed_by_month, month)