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
|
|
|
|
|
2016-06-29 17:35:34 +02:00
|
|
|
before_action :authenticate_user!, except: :commencer
|
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
|
|
|
|
2015-11-30 15:56:06 +01:00
|
|
|
def index
|
2017-04-20 10:55:54 +02:00
|
|
|
@liste ||= params[:liste] || 'a_traiter'
|
2017-04-11 17:56:11 +02:00
|
|
|
|
|
|
|
@user_dossiers = current_user.dossiers
|
|
|
|
|
|
|
|
@dossiers_filtered = case @liste
|
|
|
|
when 'brouillon'
|
2017-05-26 18:59:21 +02:00
|
|
|
@user_dossiers.state_brouillon.order_by_updated_at
|
2017-04-11 17:56:11 +02:00
|
|
|
when 'a_traiter'
|
2017-05-26 18:59:21 +02:00
|
|
|
@user_dossiers.state_en_construction.order_by_updated_at
|
2017-04-11 17:56:11 +02:00
|
|
|
when 'en_instruction'
|
2017-05-26 18:59:21 +02:00
|
|
|
@user_dossiers.state_en_instruction.order_by_updated_at
|
2017-04-11 17:56:11 +02:00
|
|
|
when 'termine'
|
2017-05-26 18:59:21 +02:00
|
|
|
@user_dossiers.state_termine.order_by_updated_at
|
2017-04-11 17:56:11 +02:00
|
|
|
when 'invite'
|
|
|
|
current_user.invites
|
|
|
|
else
|
2016-10-11 18:05:49 +02:00
|
|
|
return redirect_to users_dossiers_path
|
|
|
|
end
|
2016-01-08 11:39:04 +01:00
|
|
|
|
2018-01-30 14:43:56 +01:00
|
|
|
# FIXME: remove when
|
|
|
|
# https://github.com/Sology/smart_listing/issues/134
|
|
|
|
# is fixed
|
|
|
|
permit_smart_listing_params
|
|
|
|
# END OF FIXME
|
|
|
|
|
2016-01-08 11:39:04 +01:00
|
|
|
@dossiers = smart_listing_create :dossiers,
|
2017-06-12 13:49:51 +02:00
|
|
|
@dossiers_filtered,
|
|
|
|
partial: "users/dossiers/list",
|
|
|
|
array: true
|
2015-11-30 15:56:06 +01:00
|
|
|
end
|
|
|
|
|
2016-06-29 17:35:34 +02:00
|
|
|
def commencer
|
2018-01-11 19:04:39 +01:00
|
|
|
if params[:procedure_path].present?
|
2017-01-10 15:27:40 +01:00
|
|
|
procedure_path = ProcedurePath.where(path: params[:procedure_path]).last
|
|
|
|
|
2017-07-06 14:40:06 +02:00
|
|
|
if procedure_path.nil? || procedure_path.procedure.nil?
|
2017-01-10 15:27:40 +01:00
|
|
|
flash.alert = "Procédure inconnue"
|
|
|
|
return redirect_to root_path
|
|
|
|
else
|
|
|
|
procedure = procedure_path.procedure
|
|
|
|
end
|
2016-06-24 16:41:44 +02:00
|
|
|
end
|
2015-12-03 12:00:22 +01:00
|
|
|
|
2017-07-13 15:09:25 +02:00
|
|
|
if procedure.archivee?
|
2016-11-07 17:08:33 +01:00
|
|
|
|
|
|
|
@dossier = Dossier.new(procedure: procedure)
|
|
|
|
|
|
|
|
return render 'commencer/archived'
|
|
|
|
end
|
|
|
|
|
2016-06-29 17:35:34 +02:00
|
|
|
redirect_to new_users_dossier_path(procedure_id: procedure.id)
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
error_procedure
|
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
2017-07-11 15:52:06 +02:00
|
|
|
procedure = Procedure.publiees.find(params[:procedure_id])
|
2016-06-29 17:35:34 +02:00
|
|
|
|
2017-12-04 16:17:15 +01:00
|
|
|
dossier = Dossier.create(procedure: procedure, user: current_user, state: 'brouillon')
|
2016-06-20 13:57:57 +02:00
|
|
|
siret = params[:siret] || current_user.siret
|
2015-12-03 12:00:22 +01:00
|
|
|
|
2018-01-11 19:04:39 +01:00
|
|
|
update_current_user_siret! siret if siret.present?
|
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
|
|
|
|
redirect_to users_dossier_path(id: dossier.id)
|
|
|
|
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-01-11 19:04:39 +01:00
|
|
|
@siret = current_user.siret if current_user.siret.present?
|
2015-09-23 19:20:03 +02:00
|
|
|
|
2016-10-06 11:45:26 +02:00
|
|
|
if @facade.procedure.for_individual? && current_user.loged_in_with_france_connect?
|
|
|
|
individual = @facade.dossier.individual
|
|
|
|
|
|
|
|
individual.update_column :gender, @facade.dossier.france_connect_information.gender
|
|
|
|
individual.update_column :nom, @facade.dossier.france_connect_information.family_name
|
|
|
|
individual.update_column :prenom, @facade.dossier.france_connect_information.given_name
|
2017-07-26 10:34:16 +02:00
|
|
|
individual.update_column :birthdate, @facade.dossier.france_connect_information.birthdate.iso8601
|
2016-10-06 11:45:26 +02:00
|
|
|
end
|
|
|
|
|
2015-09-23 19:20:03 +02:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
flash.alert = t('errors.messages.dossier_not_found')
|
2015-12-03 12:00:22 +01:00
|
|
|
redirect_to url_for users_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-02-22 16:53:29 +01:00
|
|
|
etablissement_attributes = SIRETService.fetch(siret, @facade.dossier)
|
2018-03-05 16:49:17 +01:00
|
|
|
Rails.logger.info("etablissement_attributes for siret: #{siret}, present?: #{etablissement_attributes.present?}")
|
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-02-28 17:16:03 +01:00
|
|
|
if etablissement.save
|
2018-03-05 16:49:17 +01:00
|
|
|
Rails.logger.info("etablissement saved, siret: #{siret}, id: #{etablissement.id}")
|
2018-02-28 17:16:03 +01:00
|
|
|
else
|
2018-03-05 16:49:17 +01:00
|
|
|
Rails.logger.info("etablissement not saved, siret: #{siret}, errors: #{etablissement.errors.full_messages}")
|
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
|
2016-07-22 11:34:34 +02:00
|
|
|
rescue RestClient::ResourceNotFound, RestClient::BadRequest
|
2015-12-03 15:02:22 +01:00
|
|
|
errors_valid_siret
|
2016-11-01 22:56:58 +01:00
|
|
|
|
2015-09-23 19:20:03 +02:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
flash.alert = t('errors.messages.dossier_not_found')
|
2016-06-20 13:57:57 +02:00
|
|
|
redirect_to url_for users_dossiers_path
|
|
|
|
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]
|
2015-09-23 19:20:03 +02:00
|
|
|
|
2017-07-25 17:00:29 +02:00
|
|
|
if individual_errors.any?
|
|
|
|
flash.alert = individual_errors
|
|
|
|
redirect_to users_dossier_path(id: @facade.dossier.id)
|
|
|
|
else
|
2018-03-02 16:27:03 +01:00
|
|
|
if !Dossier.find(@facade.dossier.id).update update_params_with_formatted_birthdate
|
2017-07-13 09:46:13 +02:00
|
|
|
flash.alert = @facade.dossier.errors.full_messages
|
2016-12-21 15:39:41 +01:00
|
|
|
|
2016-12-20 17:02:36 +01:00
|
|
|
return redirect_to users_dossier_path(id: @facade.dossier.id)
|
2016-08-30 11:18:43 +02:00
|
|
|
end
|
2016-12-21 15:39:41 +01:00
|
|
|
|
2015-12-24 15:10:20 +01:00
|
|
|
if @facade.dossier.procedure.module_api_carto.use_api_carto
|
|
|
|
redirect_to url_for(controller: :carte, action: :show, dossier_id: @facade.dossier.id)
|
2015-09-28 18:55:16 +02:00
|
|
|
else
|
2018-02-21 18:36:06 +01:00
|
|
|
redirect_to modifier_dossier_path(@facade.dossier)
|
2015-09-28 18:55:16 +02:00
|
|
|
end
|
2015-09-23 19:20:03 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-26 15:52:05 +01:00
|
|
|
def self.route_authorization
|
|
|
|
{
|
2018-01-15 19:02:12 +01:00
|
|
|
states: [: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
|
2017-10-19 11:02:01 +02:00
|
|
|
redirect_to url_for users_dossiers_path(liste: 'brouillon')
|
2016-10-25 17:39:53 +02:00
|
|
|
end
|
|
|
|
|
2017-04-26 16:15:48 +02:00
|
|
|
def text_summary
|
|
|
|
dossier = Dossier.find(params[:dossier_id])
|
|
|
|
render json: { textSummary: dossier.text_summary }
|
2017-03-28 13:17:58 +02:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
render json: {}, status: 404
|
|
|
|
end
|
|
|
|
|
2015-09-23 19:20:03 +02:00
|
|
|
private
|
|
|
|
|
2015-12-03 15:02:22 +01:00
|
|
|
def check_siret
|
2018-01-11 19:04:39 +01:00
|
|
|
errors_valid_siret if !Siret.new(siret: siret).valid?
|
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 update_params
|
2016-10-06 11:21:07 +02:00
|
|
|
params.require(:dossier).permit(:id, :autorisation_donnees, individual_attributes: [:gender, :nom, :prenom, :birthdate])
|
2015-09-23 19:20:03 +02:00
|
|
|
end
|
|
|
|
|
2017-03-28 18:06:49 +02:00
|
|
|
def update_params_with_formatted_birthdate
|
|
|
|
editable_params = update_params
|
|
|
|
|
|
|
|
if editable_params &&
|
|
|
|
editable_params[:individual_attributes] &&
|
2017-07-25 17:26:40 +02:00
|
|
|
editable_params[:individual_attributes][:birthdate]
|
2017-03-28 18:06:49 +02:00
|
|
|
|
2017-07-25 17:26:40 +02:00
|
|
|
iso_date = begin
|
|
|
|
Date.parse(editable_params[:individual_attributes][:birthdate]).iso8601
|
|
|
|
rescue
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
editable_params[:individual_attributes][:birthdate] = iso_date
|
2017-03-28 18:06:49 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
editable_params
|
|
|
|
end
|
|
|
|
|
2017-07-25 17:00:29 +02:00
|
|
|
def individual_errors
|
|
|
|
errors = []
|
|
|
|
|
|
|
|
if update_params[:autorisation_donnees] != "1"
|
|
|
|
errors << "La validation des conditions d'utilisation est obligatoire"
|
|
|
|
end
|
|
|
|
|
|
|
|
if update_params[:individual_attributes].present? &&
|
2018-02-06 16:58:04 +01:00
|
|
|
update_params[:individual_attributes][:birthdate] &&
|
2017-07-25 17:00:29 +02:00
|
|
|
!/^\d{4}\-\d{2}\-\d{2}$/.match(update_params[:individual_attributes][:birthdate]) &&
|
|
|
|
!/^\d{2}\/\d{2}\/\d{4}$/.match(update_params[:individual_attributes][:birthdate])
|
|
|
|
errors << "Le format de la date de naissance doit être JJ/MM/AAAA"
|
|
|
|
end
|
|
|
|
|
|
|
|
errors
|
2015-09-23 19:20:03 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
2015-12-24 15:22:30 +01:00
|
|
|
redirect_to url_for users_dossiers_path
|
2015-12-03 12:00:22 +01:00
|
|
|
end
|
2016-01-19 17:19:38 +01:00
|
|
|
|
2016-06-20 13:57:57 +02: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
|
|
|
|
2016-06-20 13:57:57 +02:00
|
|
|
def facade id = params[:id]
|
|
|
|
DossierFacades.new id, current_user.email
|
2016-01-19 17:19:38 +01:00
|
|
|
end
|
2015-09-23 12:04:57 +02:00
|
|
|
end
|