2015-10-09 14:43:19 +02:00
|
|
|
class Users::DescriptionController < UsersController
|
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
|
|
|
|
2016-11-02 16:33:01 +01:00
|
|
|
before_action :check_autorisation_donnees, only: [:show]
|
|
|
|
before_action :check_starter_dossier_informations, only: [:show]
|
|
|
|
|
2015-08-10 11:05:06 +02:00
|
|
|
def show
|
2016-11-02 16:33:01 +01:00
|
|
|
@dossier ||= current_user_dossier.decorate
|
2015-08-11 15:39:16 +02:00
|
|
|
|
2015-09-21 17:59:03 +02:00
|
|
|
@procedure = @dossier.procedure
|
2015-11-04 11:14:07 +01:00
|
|
|
@champs = @dossier.ordered_champs
|
2015-08-20 12:20:54 +02:00
|
|
|
|
2017-03-29 22:17:50 +02:00
|
|
|
@headers = @champs.select { |c| c.type_champ == 'header_section' }
|
2016-12-05 19:30:58 +01:00
|
|
|
|
2018-01-11 19:04:39 +01:00
|
|
|
if !@dossier.can_be_en_construction?
|
2017-03-06 14:29:01 +01:00
|
|
|
flash[:alert] = t('errors.messages.procedure_archived')
|
|
|
|
end
|
|
|
|
|
2015-08-18 10:43:36 +02:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
2015-09-23 16:56:30 +02:00
|
|
|
flash.alert = t('errors.messages.dossier_not_found')
|
2015-10-09 17:33:33 +02:00
|
|
|
redirect_to url_for(root_path)
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|
|
|
|
|
2017-03-06 14:29:01 +01:00
|
|
|
def update
|
2017-04-10 12:29:14 +02:00
|
|
|
dossier = current_user_dossier
|
|
|
|
procedure = dossier.procedure
|
2017-03-06 14:29:01 +01:00
|
|
|
|
2018-01-11 19:04:39 +01:00
|
|
|
return head :forbidden if !dossier.can_be_en_construction?
|
2016-10-05 16:45:51 +02:00
|
|
|
|
2017-03-30 16:12:01 +02:00
|
|
|
ChampsService.save_champs(dossier.champs, params) if params[:champs]
|
2016-11-10 17:26:00 +01:00
|
|
|
|
2017-04-10 12:29:14 +02:00
|
|
|
if procedure.cerfa_flag? && params[:cerfa_pdf]
|
|
|
|
cerfa = Cerfa.new(content: params[:cerfa_pdf], dossier: dossier, user: current_user)
|
2018-01-11 19:04:39 +01:00
|
|
|
return redirect_to_description_with_errors(dossier, cerfa.errors.full_messages) if !cerfa.save
|
2015-08-11 15:39:16 +02:00
|
|
|
end
|
2015-08-10 11:05:06 +02:00
|
|
|
|
2018-01-30 19:17:16 +01:00
|
|
|
errors_upload = PiecesJustificativesService.upload!(dossier, current_user, params) + ChampsService.check_piece_justificative_files(dossier.champs)
|
2017-04-10 12:29:14 +02:00
|
|
|
return redirect_to_description_with_errors(dossier, errors_upload) if errors_upload.any?
|
2015-08-10 11:05:06 +02:00
|
|
|
|
2017-12-18 17:51:04 +01:00
|
|
|
if params[:champs] && !(brouillon_submission? || brouillon_then_dashboard_submission?)
|
2017-03-30 16:12:01 +02:00
|
|
|
errors =
|
|
|
|
ChampsService.build_error_messages(dossier.champs) +
|
|
|
|
PiecesJustificativesService.missing_pj_error_messages(dossier)
|
|
|
|
return redirect_to_description_with_errors(dossier, errors) if errors.any?
|
|
|
|
end
|
|
|
|
|
2017-12-04 16:17:15 +01:00
|
|
|
if brouillon_submission?
|
2017-03-30 09:31:46 +02:00
|
|
|
flash.notice = 'Votre brouillon a bien été sauvegardé.'
|
2017-12-18 17:51:04 +01:00
|
|
|
redirect_to users_dossier_description_path(dossier.id)
|
|
|
|
elsif brouillon_then_dashboard_submission?
|
2017-03-30 09:31:46 +02:00
|
|
|
redirect_to url_for(controller: :dossiers, action: :index, liste: :brouillon)
|
|
|
|
else
|
2017-12-04 16:17:15 +01:00
|
|
|
if dossier.brouillon?
|
2017-12-14 15:51:45 +01:00
|
|
|
dossier.en_construction!
|
2018-01-10 17:46:41 +01:00
|
|
|
# TODO move to model
|
2018-05-25 18:05:28 +02:00
|
|
|
NotificationMailer.send_initiated_notification(dossier).deliver_later
|
2016-10-05 16:45:51 +02:00
|
|
|
end
|
2016-10-27 14:02:38 +02:00
|
|
|
flash.notice = 'Félicitations, votre demande a bien été enregistrée.'
|
2017-04-10 12:29:14 +02:00
|
|
|
redirect_to url_for(controller: :recapitulatif, action: :show, dossier_id: dossier.id)
|
2016-10-05 16:45:51 +02:00
|
|
|
end
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|
|
|
|
|
2016-03-22 17:36:36 +01:00
|
|
|
def pieces_justificatives
|
|
|
|
invite = current_user.invite? params[:dossier_id]
|
|
|
|
|
|
|
|
@dossier ||= Dossier.find(params[:dossier_id]) if invite
|
|
|
|
@dossier ||= current_user_dossier
|
|
|
|
|
2016-11-10 17:26:00 +01:00
|
|
|
if @dossier.procedure.cerfa_flag?
|
2018-01-11 19:04:39 +01:00
|
|
|
if params[:cerfa_pdf].present?
|
2016-11-10 17:26:00 +01:00
|
|
|
cerfa = Cerfa.new(content: params[:cerfa_pdf], dossier: @dossier, user: current_user)
|
2018-01-11 19:04:39 +01:00
|
|
|
if !cerfa.save
|
2017-07-13 09:46:13 +02:00
|
|
|
flash.alert = cerfa.errors.full_messages
|
2016-11-10 17:26:00 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-11 20:06:38 +01:00
|
|
|
if (errors_upload = PiecesJustificativesService.upload!(@dossier, current_user, params)).present?
|
2016-11-10 17:26:00 +01:00
|
|
|
if flash.alert.nil?
|
2017-07-13 09:46:13 +02:00
|
|
|
flash.alert = errors_upload
|
2016-11-10 17:26:00 +01:00
|
|
|
else
|
2017-07-13 09:46:13 +02:00
|
|
|
flash.alert = [flash.alert] + errors_upload
|
2016-11-10 17:26:00 +01:00
|
|
|
end
|
|
|
|
|
2016-03-22 17:36:36 +01:00
|
|
|
else
|
2016-11-10 17:26:00 +01:00
|
|
|
flash.notice = 'Nouveaux fichiers envoyés' if flash.alert.nil?
|
2016-03-22 17:36:36 +01:00
|
|
|
end
|
|
|
|
|
2018-03-06 13:44:29 +01:00
|
|
|
return redirect_to users_dossiers_invite_path(id: current_user.invites.find_by(dossier_id: @dossier.id).id) if invite
|
2016-03-22 17:36:36 +01:00
|
|
|
|
|
|
|
redirect_to users_dossier_recapitulatif_path
|
|
|
|
end
|
|
|
|
|
2016-01-26 15:52:05 +01:00
|
|
|
def self.route_authorization
|
|
|
|
{
|
2018-01-15 19:02:12 +01:00
|
|
|
states: [:brouillon, :en_construction]
|
2016-01-26 15:52:05 +01:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-08-10 11:05:06 +02:00
|
|
|
private
|
|
|
|
|
2017-03-30 09:50:30 +02:00
|
|
|
def redirect_to_description_with_errors(dossier, errors)
|
2017-10-11 17:32:45 +02:00
|
|
|
errors_to_display = if errors.count > 3
|
|
|
|
errors.take(3) + ['...']
|
|
|
|
else
|
|
|
|
errors
|
|
|
|
end
|
|
|
|
|
|
|
|
flash.alert = errors_to_display
|
2017-03-30 09:50:30 +02:00
|
|
|
redirect_to users_dossier_description_path(dossier_id: dossier.id)
|
|
|
|
end
|
|
|
|
|
2017-12-04 16:17:15 +01:00
|
|
|
def brouillon_submission?
|
2018-02-15 18:38:58 +01:00
|
|
|
params[:submit_action] == 'brouillon'
|
2017-12-18 17:51:04 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def brouillon_then_dashboard_submission?
|
2018-02-15 18:38:58 +01:00
|
|
|
params[:submit_action] == 'brouillon_then_dashboard'
|
2017-03-29 23:18:37 +02:00
|
|
|
end
|
|
|
|
|
2016-11-02 16:33:01 +01:00
|
|
|
def check_autorisation_donnees
|
2017-01-03 12:53:27 +01:00
|
|
|
@dossier = current_user_dossier
|
2016-11-02 16:33:01 +01:00
|
|
|
|
|
|
|
redirect_to url_for(users_dossier_path(@dossier.id)) if @dossier.autorisation_donnees.nil? || !@dossier.autorisation_donnees
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_starter_dossier_informations
|
|
|
|
@dossier ||= current_user_dossier
|
|
|
|
|
|
|
|
if (@dossier.procedure.for_individual? && @dossier.individual.nil?) ||
|
2018-04-23 12:01:03 +02:00
|
|
|
(!@dossier.procedure.for_individual? && @dossier.etablissement.nil?)
|
2016-11-02 16:33:01 +01:00
|
|
|
redirect_to url_for(users_dossier_path(@dossier.id))
|
|
|
|
end
|
|
|
|
end
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|