diff --git a/app/models/etablissement.rb b/app/models/etablissement.rb index bf870e110..ef3b47d6c 100644 --- a/app/models/etablissement.rb +++ b/app/models/etablissement.rb @@ -66,6 +66,16 @@ class Etablissement < ApplicationRecord fermé: "fermé" }, _prefix: true + def entreprise_raison_sociale + read_attribute(:entreprise_raison_sociale).presence || raison_sociale_for_ei + end + + def raison_sociale_for_ei + if entreprise_nom || entreprise_prenom + [entreprise_nom, entreprise_prenom].join(' ') + end + end + def search_terms [ entreprise_siren, diff --git a/spec/models/etablissement_spec.rb b/spec/models/etablissement_spec.rb index dce441520..a7f38d6cd 100644 --- a/spec/models/etablissement_spec.rb +++ b/spec/models/etablissement_spec.rb @@ -36,6 +36,30 @@ describe Etablissement do end end + describe '#entreprise_raison_sociale' do + subject { etablissement.entreprise_raison_sociale } + + context "with nom and prenom" do + context "without raison sociale" do + let(:etablissement) { create(:etablissement, entreprise_raison_sociale: nil, entreprise_prenom: "Stef", entreprise_nom: "Sanseverino") } + + it { is_expected.to eq "Sanseverino Stef" } + end + + context "with raison sociale" do + let(:etablissement) { create(:etablissement, entreprise_raison_sociale: "Sansev Prod", entreprise_prenom: "Stef", entreprise_nom: "Sanseverino") } + + it { is_expected.to eq "Sansev Prod" } + end + end + + context "without nom and prenom" do + let(:etablissement) { create(:etablissement, entreprise_raison_sociale: "ENGIE", entreprise_prenom: nil, entreprise_nom: nil) } + + it { is_expected.to eq "ENGIE" } + end + end + describe '.entreprise_bilans_bdf_to_csv' do let(:etablissement) { build(:etablissement, entreprise_bilans_bdf: bilans) } let(:ordered_headers) {