2015-09-23 19:20:03 +02:00
|
|
|
class Users::DossiersController < UsersController
|
2016-01-08 11:39:04 +01:00
|
|
|
include SmartListing::Helper::ControllerExtensions
|
|
|
|
helper SmartListing::Helper
|
|
|
|
|
2018-05-14 18:12:07 +02:00
|
|
|
SESSION_USER_RETURN_LOCATION = 'user_return_to'
|
|
|
|
|
|
|
|
before_action :store_user_location!, only: :new
|
2018-08-14 11:47:47 +02:00
|
|
|
before_action :authenticate_user!, except: [:commencer, :commencer_test]
|
2016-06-20 13:57:57 +02:00
|
|
|
before_action :check_siret, only: :siret_informations
|
2015-11-03 15:27:49 +01:00
|
|
|
|
2016-01-26 15:52:05 +01:00
|
|
|
before_action only: [:show] do
|
|
|
|
authorized_routes? self.class
|
|
|
|
end
|
2016-01-25 15:54:21 +01:00
|
|
|
|
2018-05-17 15:34:51 +02:00
|
|
|
def commencer_test
|
|
|
|
procedure_path = ProcedurePath.find_by(path: params[:procedure_path])
|
2018-08-14 11:47:47 +02:00
|
|
|
procedure = procedure_path&.procedure
|
2017-01-10 15:27:40 +01:00
|
|
|
|
2018-10-09 15:05:08 +02:00
|
|
|
if procedure&.brouillon? && procedure&.path.present?
|
2018-08-14 11:47:47 +02:00
|
|
|
redirect_to new_users_dossier_path(procedure_id: procedure.id, brouillon: true)
|
2018-05-17 15:34:51 +02:00
|
|
|
else
|
2018-09-05 14:48:42 +02:00
|
|
|
flash.alert = "La démarche est inconnue."
|
2018-05-17 15:34:51 +02:00
|
|
|
redirect_to root_path
|
2016-06-24 16:41:44 +02:00
|
|
|
end
|
2018-05-17 15:34:51 +02:00
|
|
|
end
|
2015-12-03 12:00:22 +01:00
|
|
|
|
2018-05-17 15:34:51 +02:00
|
|
|
def commencer
|
|
|
|
procedure_path = ProcedurePath.find_by(path: params[:procedure_path])
|
2018-05-23 15:36:50 +02:00
|
|
|
procedure = procedure_path&.procedure
|
2016-11-07 17:08:33 +01:00
|
|
|
|
2018-05-17 15:34:51 +02:00
|
|
|
if procedure.present?
|
|
|
|
if procedure.archivee?
|
|
|
|
@dossier = Dossier.new(procedure: procedure)
|
2016-11-07 17:08:33 +01:00
|
|
|
|
2018-05-17 15:34:51 +02:00
|
|
|
render 'commencer/archived'
|
|
|
|
else
|
|
|
|
redirect_to new_users_dossier_path(procedure_id: procedure.id)
|
|
|
|
end
|
|
|
|
else
|
2018-09-05 14:48:42 +02:00
|
|
|
flash.alert = "La démarche est inconnue, ou la création de nouveaux dossiers pour cette démarche est terminée."
|
2018-05-17 15:34:51 +02:00
|
|
|
redirect_to root_path
|
2016-11-07 17:08:33 +01:00
|
|
|
end
|
2016-06-29 17:35:34 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
2018-05-14 18:12:07 +02:00
|
|
|
erase_user_location!
|
|
|
|
|
2018-08-14 11:47:47 +02:00
|
|
|
if params[:brouillon]
|
|
|
|
procedure = Procedure.brouillon.find(params[:procedure_id])
|
|
|
|
else
|
|
|
|
procedure = Procedure.publiees.find(params[:procedure_id])
|
|
|
|
end
|
2016-06-29 17:35:34 +02:00
|
|
|
|
2018-08-28 14:10:55 +02:00
|
|
|
dossier = Dossier.create!(procedure: procedure, user: current_user, state: Dossier.states.fetch(:brouillon))
|
2016-06-20 13:57:57 +02:00
|
|
|
siret = params[:siret] || current_user.siret
|
2015-12-03 12:00:22 +01:00
|
|
|
|
2018-10-01 13:24:37 +02:00
|
|
|
if siret.present?
|
|
|
|
update_current_user_siret! siret
|
|
|
|
end
|
2016-06-20 13:57:57 +02:00
|
|
|
|
2018-02-12 15:55:51 +01:00
|
|
|
if dossier.procedure.for_individual
|
|
|
|
redirect_to identite_dossier_path(dossier)
|
|
|
|
else
|
2018-10-04 09:43:19 +02:00
|
|
|
redirect_to siret_dossier_path(id: dossier.id)
|
2018-02-12 15:55:51 +01:00
|
|
|
end
|
2015-12-03 12:00:22 +01:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
error_procedure
|
|
|
|
end
|
|
|
|
|
2015-11-26 12:29:34 +01:00
|
|
|
def show
|
2016-06-20 13:57:57 +02:00
|
|
|
@facade = facade
|
2018-10-01 13:24:37 +02:00
|
|
|
if current_user.siret.present?
|
|
|
|
@siret = current_user.siret
|
|
|
|
end
|
2015-09-23 19:20:03 +02:00
|
|
|
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
flash.alert = t('errors.messages.dossier_not_found')
|
2018-06-27 14:47:02 +02:00
|
|
|
redirect_to url_for dossiers_path
|
2015-09-23 19:20:03 +02:00
|
|
|
end
|
|
|
|
|
2016-06-20 13:57:57 +02:00
|
|
|
def siret_informations
|
|
|
|
@facade = facade params[:dossier_id]
|
2015-09-23 19:20:03 +02:00
|
|
|
|
2018-02-22 16:53:29 +01:00
|
|
|
update_current_user_siret!(siret)
|
2015-12-11 12:36:44 +01:00
|
|
|
|
2018-04-23 12:01:03 +02:00
|
|
|
etablissement_attributes = ApiEntrepriseService.get_etablissement_params_for_siret(siret, @facade.dossier.procedure_id)
|
2016-07-22 11:34:34 +02:00
|
|
|
|
2018-02-28 17:16:03 +01:00
|
|
|
if etablissement_attributes.present?
|
|
|
|
etablissement_attributes = ActionController::Parameters.new(etablissement_attributes).permit!
|
2018-03-07 17:43:44 +01:00
|
|
|
etablissement = @facade.dossier.build_etablissement(etablissement_attributes)
|
2018-03-26 17:44:51 +02:00
|
|
|
if !etablissement.save
|
2018-02-28 17:16:03 +01:00
|
|
|
return errors_valid_siret
|
|
|
|
end
|
2018-02-22 16:53:29 +01:00
|
|
|
else
|
2016-07-22 11:34:34 +02:00
|
|
|
return errors_valid_siret
|
|
|
|
end
|
2015-09-23 19:20:03 +02:00
|
|
|
|
2016-06-20 13:57:57 +02:00
|
|
|
@facade = facade params[:dossier_id]
|
2015-09-23 19:20:03 +02:00
|
|
|
|
2016-10-05 14:28:10 +02:00
|
|
|
if @facade.procedure.individual_with_siret?
|
|
|
|
render '/dossiers/add_siret', formats: 'js'
|
|
|
|
else
|
|
|
|
render '/dossiers/new_siret', formats: 'js'
|
|
|
|
end
|
2015-09-23 19:20:03 +02:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
flash.alert = t('errors.messages.dossier_not_found')
|
2018-06-27 14:47:02 +02:00
|
|
|
redirect_to url_for dossiers_path
|
2016-06-20 13:57:57 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def change_siret
|
|
|
|
Dossier.find(params[:dossier_id]).reset!
|
|
|
|
|
|
|
|
@facade = facade params[:dossier_id]
|
|
|
|
|
2016-10-05 14:28:10 +02:00
|
|
|
if @facade.procedure.individual_with_siret?
|
|
|
|
render '/dossiers/add_siret', formats: 'js'
|
|
|
|
else
|
|
|
|
render '/dossiers/new_siret', formats: 'js'
|
|
|
|
end
|
2015-09-23 19:20:03 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2016-06-20 13:57:57 +02:00
|
|
|
@facade = facade params[:dossier][:id]
|
2018-10-02 13:51:56 +02:00
|
|
|
@facade.dossier.update!(autorisation_donnees: true)
|
2015-09-23 19:20:03 +02:00
|
|
|
|
2018-10-02 13:51:56 +02:00
|
|
|
if @facade.dossier.procedure.module_api_carto.use_api_carto
|
|
|
|
redirect_to url_for(controller: :carte, action: :show, dossier_id: @facade.dossier.id)
|
2017-07-25 17:00:29 +02:00
|
|
|
else
|
2018-10-02 13:51:56 +02:00
|
|
|
redirect_to brouillon_dossier_path(@facade.dossier)
|
2015-09-23 19:20:03 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-26 15:52:05 +01:00
|
|
|
def self.route_authorization
|
|
|
|
{
|
2018-08-28 14:12:48 +02:00
|
|
|
states: [Dossier.states.fetch(:brouillon)]
|
2016-01-26 15:52:05 +01:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-10-25 17:39:53 +02:00
|
|
|
def destroy
|
2016-10-27 13:46:12 +02:00
|
|
|
dossier = current_user.dossiers.find(params[:id])
|
2016-10-25 17:39:53 +02:00
|
|
|
if dossier.brouillon?
|
|
|
|
dossier.destroy
|
|
|
|
flash.notice = 'Brouillon supprimé'
|
|
|
|
end
|
2018-06-27 14:47:02 +02:00
|
|
|
redirect_to url_for dossiers_path
|
2016-10-25 17:39:53 +02:00
|
|
|
end
|
|
|
|
|
2015-09-23 19:20:03 +02:00
|
|
|
private
|
|
|
|
|
2015-12-03 15:02:22 +01:00
|
|
|
def check_siret
|
2018-10-01 13:24:37 +02:00
|
|
|
if !Siret.new(siret: siret).valid?
|
|
|
|
errors_valid_siret
|
|
|
|
end
|
2015-12-03 15:02:22 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def errors_valid_siret
|
|
|
|
flash.alert = t('errors.messages.invalid_siret')
|
2016-06-20 13:57:57 +02:00
|
|
|
@facade = facade params[:dossier_id]
|
|
|
|
|
2018-01-16 13:34:24 +01:00
|
|
|
render '/dossiers/new_siret', formats: :js, locals: { invalid_siret: siret }
|
2015-12-03 15:02:22 +01:00
|
|
|
end
|
|
|
|
|
2015-09-23 19:20:03 +02:00
|
|
|
def siret
|
2015-12-03 12:00:22 +01:00
|
|
|
create_params[:siret]
|
2015-09-23 19:20:03 +02:00
|
|
|
end
|
|
|
|
|
2015-12-03 12:00:22 +01:00
|
|
|
def create_params
|
2016-06-20 13:57:57 +02:00
|
|
|
params.require(:dossier).permit(:siret)
|
2015-12-03 12:00:22 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def error_procedure
|
2015-12-24 15:22:30 +01:00
|
|
|
flash.alert = t('errors.messages.procedure_not_found')
|
2016-06-20 13:57:57 +02:00
|
|
|
|
2018-06-27 14:47:02 +02:00
|
|
|
redirect_to url_for dossiers_path
|
2015-12-03 12:00:22 +01:00
|
|
|
end
|
2016-01-19 17:19:38 +01:00
|
|
|
|
2018-03-20 17:47:37 +01:00
|
|
|
def update_current_user_siret!(siret)
|
2018-03-02 16:27:03 +01:00
|
|
|
current_user.update(siret: siret)
|
2016-06-20 13:57:57 +02:00
|
|
|
end
|
2016-01-19 17:19:38 +01:00
|
|
|
|
2018-03-20 17:47:37 +01:00
|
|
|
def facade(id = params[:id])
|
2016-06-20 13:57:57 +02:00
|
|
|
DossierFacades.new id, current_user.email
|
2016-01-19 17:19:38 +01:00
|
|
|
end
|
2018-05-14 18:12:07 +02:00
|
|
|
|
|
|
|
def store_user_location!
|
|
|
|
store_location_for(:user, request.fullpath)
|
|
|
|
end
|
|
|
|
|
|
|
|
def erase_user_location!
|
|
|
|
session.delete(SESSION_USER_RETURN_LOCATION)
|
|
|
|
end
|
2015-09-23 12:04:57 +02:00
|
|
|
end
|