diff --git a/app/controllers/users/dossiers/add_siret_controller.rb b/app/controllers/users/dossiers/add_siret_controller.rb new file mode 100644 index 000000000..5e6591197 --- /dev/null +++ b/app/controllers/users/dossiers/add_siret_controller.rb @@ -0,0 +1,13 @@ +class Users::Dossiers::AddSiretController < ApplicationController + def show + @facade = DossierFacades.new params[:dossier_id], current_user.email + + raise ActiveRecord::RecordNotFound unless @facade.procedure.individual_with_siret? + + @siret = current_user.siret unless current_user.siret.nil? + + rescue ActiveRecord::RecordNotFound + flash.alert = t('errors.messages.dossier_not_found') + redirect_to url_for users_dossiers_path + end +end \ No newline at end of file diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index bf81eae8c..a0dac2e30 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -65,8 +65,12 @@ class Users::DossiersController < UsersController end @facade = facade params[:dossier_id] - render '/dossiers/new_siret', formats: 'js' + if @facade.procedure.individual_with_siret? + render '/dossiers/add_siret', formats: 'js' + else + render '/dossiers/new_siret', formats: 'js' + end rescue RestClient::ResourceNotFound, RestClient::BadRequest errors_valid_siret @@ -80,7 +84,11 @@ class Users::DossiersController < UsersController @facade = facade params[:dossier_id] - render '/dossiers/new_siret', formats: :js + if @facade.procedure.individual_with_siret? + render '/dossiers/add_siret', formats: 'js' + else + render '/dossiers/new_siret', formats: 'js' + end end def update diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 9306e2e3e..3a17e04a7 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -73,8 +73,6 @@ class Dossier < ActiveRecord::Base def build_default_individual Individual.new(dossier_id: id).save(validate: false) - Entreprise.new(dossier_id: id).save(validate: false) - Etablissement.new(dossier_id: id, entreprise_id: entreprise.id).save(validate: false) end def ordered_champs @@ -289,7 +287,8 @@ class Dossier < ActiveRecord::Base end def reset! - entreprise.destroy unless entreprise.nil? + entreprise.destroy + etablissement.destroy update_attributes(autorisation_donnees: false) end diff --git a/app/models/entreprise.rb b/app/models/entreprise.rb index 8218f75e4..5e24a31bb 100644 --- a/app/models/entreprise.rb +++ b/app/models/entreprise.rb @@ -4,6 +4,7 @@ class Entreprise < ActiveRecord::Base has_one :rna_information, dependent: :destroy validates_presence_of :siren + validates_uniqueness_of :dossier_id before_save :default_values diff --git a/app/models/etablissement.rb b/app/models/etablissement.rb index 9d09b9787..44e840205 100644 --- a/app/models/etablissement.rb +++ b/app/models/etablissement.rb @@ -4,6 +4,8 @@ class Etablissement < ActiveRecord::Base has_many :exercices, dependent: :destroy + validates_uniqueness_of :dossier_id + def geo_adresse [numero_voie, type_voie, nom_voie, complement_adresse, code_postal, localite].join(' ') end diff --git a/app/views/backoffice/dossiers/show.html.haml b/app/views/backoffice/dossiers/show.html.haml index 6eaa95dde..9d0288dd9 100644 --- a/app/views/backoffice/dossiers/show.html.haml +++ b/app/views/backoffice/dossiers/show.html.haml @@ -7,7 +7,7 @@ = @facade.dossier.display_state = render partial: 'follow_action' - - unless @facade.procedure.for_individual? + - unless @facade.entreprise.nil? = render partial: '/dossiers/infos_entreprise' = render partial: '/dossiers/infos_dossier' diff --git a/app/views/dossiers/_infos_dossier.html.haml b/app/views/dossiers/_infos_dossier.html.haml index 539f09e05..2b349ef4b 100644 --- a/app/views/dossiers/_infos_dossier.html.haml +++ b/app/views/dossiers/_infos_dossier.html.haml @@ -54,6 +54,9 @@ = render partial: 'users/recapitulatif/modal_upload_pj' + -if @facade.procedure.individual_with_siret + %a#add_siret.btn.btn-success{href: users_dossier_add_siret_path(dossier_id: @facade.dossier.id)} + = "Renseigner un SIRET" -if @facade.dossier.procedure.module_api_carto.use_api_carto %a#maj_carte.btn.btn-primary{href: "/users/dossiers/#{@facade.dossier.id}/carte"} = 'Modifier la carte' diff --git a/app/views/dossiers/add_siret.js.erb b/app/views/dossiers/add_siret.js.erb new file mode 100644 index 000000000..72bb92d20 --- /dev/null +++ b/app/views/dossiers/add_siret.js.erb @@ -0,0 +1,12 @@ +<% if flash.empty? %> +$('.row.etape.etape_2').hide(300, render_new_siret); +$('.row.etape.etape_2').slideDown(400, the_terms); +<% else %> +error_form_siret('<%= invalid_siret %>'); +<% end %> + +<% flash.clear %> + +function render_new_siret(){ + $('.row.etape.etape_2').html("<%= escape_javascript(render partial: '/dossiers/etapes/etape_2/entreprise', locals: { facade: @facade } ) %>"); +} \ No newline at end of file diff --git a/app/views/dossiers/new_siret.js.erb b/app/views/dossiers/new_siret.js.erb index c3d7503cf..dcb6ce2b2 100644 --- a/app/views/dossiers/new_siret.js.erb +++ b/app/views/dossiers/new_siret.js.erb @@ -8,7 +8,6 @@ error_form_siret('<%= invalid_siret %>'); <% flash.clear %> - function render_new_siret(){ $('.row.etape.etape_2').html("<%= escape_javascript(render partial: '/dossiers/etapes/etape2', locals: { facade: @facade } ) %>"); } \ No newline at end of file diff --git a/app/views/users/dossiers/add_siret/show.html.haml b/app/views/users/dossiers/add_siret/show.html.haml new file mode 100644 index 000000000..56d7c3162 --- /dev/null +++ b/app/views/users/dossiers/add_siret/show.html.haml @@ -0,0 +1,2 @@ +.row.etape.etape_2 + = render partial: 'dossiers/etapes/etape_2/entreprise' \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 8fe43d22d..744c3ea61 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -62,6 +62,8 @@ Rails.application.routes.draw do end resources :dossiers do + get '/add_siret' => 'dossiers/add_siret#show' + get '/description' => 'description#show' get '/description/error' => 'description#error' post 'description' => 'description#create' diff --git a/spec/controllers/users/dossiers/add_siret_controller_spec.rb b/spec/controllers/users/dossiers/add_siret_controller_spec.rb new file mode 100644 index 000000000..44083e035 --- /dev/null +++ b/spec/controllers/users/dossiers/add_siret_controller_spec.rb @@ -0,0 +1,25 @@ +require 'spec_helper' + +describe Users::Dossiers::AddSiretController, type: :controller do + describe '#GET show' do + + let(:dossier) { create :dossier } + + before do + sign_in dossier.user + end + + subject { get :show, dossier_id: dossier.id } + + context 'when dossier is not attached at a procedure with individual siret attribut' do + it { is_expected.to redirect_to users_dossiers_path } + end + + context 'when dossier is attached at a procedure with individual siret attribut' do + let(:procedure) { create :procedure, individual_with_siret: true } + let(:dossier) { create :dossier, procedure: procedure } + + it { expect(subject.status).to eq 200 } + end + end +end diff --git a/spec/views/users/recapitulatif/show.html.haml_spec.rb b/spec/views/users/recapitulatif/show.html.haml_spec.rb index f5a7bd92a..e70e3723b 100644 --- a/spec/views/users/recapitulatif/show.html.haml_spec.rb +++ b/spec/views/users/recapitulatif/show.html.haml_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' describe 'users/recapitulatif/show.html.haml', type: :view do - let(:dossier) { create(:dossier, :with_entreprise, state: state, procedure: create(:procedure, :with_api_carto, :with_two_type_de_piece_justificative)) } + let(:dossier) { create(:dossier, :with_entreprise, state: state, procedure: create(:procedure, :with_api_carto, :with_two_type_de_piece_justificative, for_individual: true, individual_with_siret: true)) } let(:dossier_id) { dossier.id } let(:state) { 'draft' } @@ -46,6 +46,12 @@ describe 'users/recapitulatif/show.html.haml', type: :view do end end + context 'lien carte' do + it 'le lien vers le renseignement un SIRET est présent' do + expect(rendered).to have_css('#add_siret') + end + end + context 'lien carte' do it 'le lien vers carte est présent' do expect(rendered).to have_css('#maj_carte')