2019-08-06 11:02:54 +02:00
module Instructeurs
2017-06-29 15:31:29 +02:00
class DossiersController < ProceduresController
2017-12-01 12:57:01 +01:00
include ActionView :: Helpers :: NumberHelper
2017-11-29 17:11:50 +01:00
include ActionView :: Helpers :: TextHelper
2018-10-31 14:56:01 +01:00
include CreateAvisConcern
2019-07-11 11:37:59 +02:00
include DossierHelper
2017-11-29 17:11:50 +01:00
2019-07-01 15:55:37 +02:00
include ActionController :: Streaming
include Zipline
2017-12-05 16:07:05 +01:00
after_action :mark_demande_as_read , only : :show
2017-12-05 17:20:10 +01:00
after_action :mark_messagerie_as_read , only : [ :messagerie , :create_commentaire ]
2017-12-07 11:15:51 +01:00
after_action :mark_avis_as_read , only : [ :avis , :create_avis ]
2017-12-07 14:37:34 +01:00
after_action :mark_annotations_privees_as_read , only : [ :annotations_privees , :update_annotations ]
2017-12-05 16:07:05 +01:00
2017-06-29 15:31:29 +02:00
def attestation
2019-09-10 11:09:51 +02:00
if dossier . attestation . pdf . attached?
2019-12-19 18:15:37 +01:00
redirect_to dossier . attestation . pdf . service_url
2019-08-27 17:43:33 +02:00
end
2017-06-29 15:31:29 +02:00
end
2020-04-30 15:49:43 +02:00
def geo_data
send_data dossier . to_feature_collection . to_json ,
type : 'application/json' ,
filename : " dossier- #{ dossier . id } -features.json "
end
2019-01-18 14:20:30 +01:00
def apercu_attestation
2019-08-28 14:27:41 +02:00
@attestation = dossier . procedure . attestation_template . render_attributes_for ( dossier : dossier )
2019-08-27 17:44:02 +02:00
2020-07-29 12:11:30 +02:00
render 'new_administrateur/attestation_templates/show' , formats : [ :pdf ]
2019-01-18 14:20:30 +01:00
end
2020-04-30 11:25:22 +02:00
def bilans_bdf
2020-06-04 12:32:09 +02:00
extension = params [ :format ]
render extension . to_sym = > dossier . etablissement . entreprise_bilans_bdf_to_sheet ( extension )
2020-04-30 11:25:22 +02:00
end
2017-07-06 16:33:36 +02:00
def show
2019-08-06 11:02:54 +02:00
@demande_seen_at = current_instructeur . follows . find_by ( dossier : dossier ) & . demande_seen_at
2019-12-15 22:09:23 +01:00
respond_to do | format |
format . pdf do
@include_infos_administration = true
2021-02-17 19:02:31 +01:00
render ( template : 'dossiers/show' , formats : [ :pdf ] )
2019-12-15 22:09:23 +01:00
end
format . all
end
2017-07-06 16:33:36 +02:00
end
2017-07-19 11:08:53 +02:00
def messagerie
2017-10-30 17:21:05 +01:00
@commentaire = Commentaire . new
2019-08-06 11:02:54 +02:00
@messagerie_seen_at = current_instructeur . follows . find_by ( dossier : dossier ) & . messagerie_seen_at
2017-07-19 11:08:53 +02:00
end
2017-09-27 12:08:47 +02:00
def annotations_privees
2019-08-06 11:02:54 +02:00
@annotations_privees_seen_at = current_instructeur . follows . find_by ( dossier : dossier ) & . annotations_privees_seen_at
2017-07-19 16:53:31 +02:00
end
2017-09-26 14:38:16 +02:00
def avis
2019-08-06 11:02:54 +02:00
@avis_seen_at = current_instructeur . follows . find_by ( dossier : dossier ) & . avis_seen_at
2018-02-27 16:58:22 +01:00
@avis = Avis . new
2021-04-16 11:11:39 +02:00
if @dossier . procedure . experts_require_administrateur_invitation?
2021-04-09 12:56:09 +02:00
@experts_emails = dossier . procedure . experts_procedures . where ( revoked_at : nil ) . map ( & :expert ) . map ( & :email ) . sort
end
2017-09-26 14:38:16 +02:00
end
2017-12-01 15:03:19 +01:00
def personnes_impliquees
2019-10-15 18:32:18 +02:00
@following_instructeurs_emails = dossier . followers_instructeurs . map ( & :email )
2019-08-06 11:02:54 +02:00
previous_followers = dossier . previous_followers_instructeurs - dossier . followers_instructeurs
2019-10-15 18:32:18 +02:00
@previous_following_instructeurs_emails = previous_followers . map ( & :email )
2021-02-25 09:53:09 +01:00
@avis_emails = dossier . experts . map ( & :email )
2017-12-01 15:03:19 +01:00
@invites_emails = dossier . invites . map ( & :email )
2019-10-24 15:07:52 +02:00
@potential_recipients = dossier . groupe_instructeur . instructeurs . reject { | g | g == current_instructeur }
2018-01-30 19:11:07 +01:00
end
2018-09-10 17:52:49 +02:00
def send_to_instructeurs
2021-03-03 17:31:05 +01:00
recipients = params [ 'recipients' ] . presence || [ ] . to_json
recipients = Instructeur . find ( JSON . parse ( recipients ) )
2018-09-10 17:52:49 +02:00
recipients . each do | recipient |
2019-11-19 16:01:43 +01:00
recipient . follow ( dossier )
2019-08-06 11:02:54 +02:00
InstructeurMailer . send_dossier ( current_instructeur , dossier , recipient ) . deliver_later
2018-09-10 17:52:49 +02:00
end
2018-01-30 19:11:07 +01:00
flash . notice = " Dossier envoyé "
2019-08-06 11:02:54 +02:00
redirect_to ( personnes_impliquees_instructeur_dossier_path ( procedure , dossier ) )
2017-12-01 15:03:19 +01:00
end
2017-07-11 15:45:20 +02:00
def follow
2019-08-06 11:02:54 +02:00
current_instructeur . follow ( dossier )
2017-07-17 12:28:09 +02:00
flash . notice = 'Dossier suivi'
2019-08-06 11:02:54 +02:00
redirect_back ( fallback_location : instructeur_procedures_url )
2017-07-11 15:45:20 +02:00
end
def unfollow
2019-08-06 11:02:54 +02:00
current_instructeur . unfollow ( dossier )
2017-07-17 12:28:09 +02:00
flash . notice = " Vous ne suivez plus le dossier nº #{ dossier . id } "
2019-08-06 11:02:54 +02:00
redirect_back ( fallback_location : instructeur_procedures_url )
2017-07-11 15:45:20 +02:00
end
2017-07-11 15:56:23 +02:00
def archive
2020-09-09 15:04:58 +02:00
dossier . archiver! ( current_instructeur )
2019-08-06 11:02:54 +02:00
redirect_back ( fallback_location : instructeur_procedures_url )
2017-07-11 15:56:23 +02:00
end
def unarchive
2020-09-09 15:04:58 +02:00
dossier . desarchiver! ( current_instructeur )
2019-08-06 11:02:54 +02:00
redirect_back ( fallback_location : instructeur_procedures_url )
2017-07-11 15:56:23 +02:00
end
2017-11-30 17:11:46 +01:00
def passer_en_instruction
2019-12-19 13:25:32 +01:00
begin
2019-08-06 11:02:54 +02:00
dossier . passer_en_instruction! ( current_instructeur )
2019-07-08 17:58:45 +02:00
flash . notice = 'Dossier passé en instruction.'
2019-12-19 13:25:32 +01:00
rescue AASM :: InvalidTransition = > e
flash . alert = aasm_error_message ( e , target_state : :en_instruction )
2019-07-08 17:58:45 +02:00
end
2017-11-30 17:11:46 +01:00
2018-09-13 17:13:09 +02:00
render partial : 'state_button_refresh' , locals : { dossier : dossier }
2017-11-30 17:11:46 +01:00
end
2017-11-30 17:18:06 +01:00
def repasser_en_construction
2019-12-19 13:25:32 +01:00
begin
2019-08-06 11:02:54 +02:00
dossier . repasser_en_construction! ( current_instructeur )
2019-07-11 11:47:37 +02:00
flash . notice = 'Dossier repassé en construction.'
2019-12-19 13:25:32 +01:00
rescue AASM :: InvalidTransition = > e
flash . alert = aasm_error_message ( e , target_state : :en_construction )
2019-07-11 11:47:37 +02:00
end
2017-11-30 17:18:06 +01:00
2018-09-13 17:13:09 +02:00
render partial : 'state_button_refresh' , locals : { dossier : dossier }
2017-11-30 17:18:06 +01:00
end
2019-07-01 16:16:40 +02:00
def repasser_en_instruction
2019-12-19 13:25:32 +01:00
begin
2019-10-02 11:29:30 +02:00
flash . notice = " Le dossier #{ dossier . id } a été repassé en instruction. "
dossier . repasser_en_instruction! ( current_instructeur )
2019-12-19 13:25:32 +01:00
rescue AASM :: InvalidTransition = > e
flash . alert = aasm_error_message ( e , target_state : :en_instruction )
2019-07-11 11:52:24 +02:00
end
2019-07-01 16:16:40 +02:00
render partial : 'state_button_refresh' , locals : { dossier : dossier }
end
2017-12-01 12:57:01 +01:00
def terminer
2018-11-26 21:24:18 +01:00
motivation = params [ :dossier ] && params [ :dossier ] [ :motivation ]
2019-02-18 17:52:15 +01:00
justificatif = params [ :dossier ] && params [ :dossier ] [ :justificatif_motivation ]
2017-12-01 12:57:01 +01:00
2019-12-19 13:25:32 +01:00
begin
2019-07-11 11:37:59 +02:00
case params [ :process_action ]
when " refuser "
2019-12-19 13:25:32 +01:00
target_state = :refuse
2019-08-06 11:02:54 +02:00
dossier . refuser! ( current_instructeur , motivation , justificatif )
2019-07-11 11:37:59 +02:00
flash . notice = " Dossier considéré comme refusé. "
when " classer_sans_suite "
2019-12-19 13:25:32 +01:00
target_state = :sans_suite
2019-08-06 11:02:54 +02:00
dossier . classer_sans_suite! ( current_instructeur , motivation , justificatif )
2019-07-11 11:37:59 +02:00
flash . notice = " Dossier considéré comme sans suite. "
when " accepter "
2019-12-19 13:25:32 +01:00
target_state = :accepte
2019-08-06 11:02:54 +02:00
dossier . accepter! ( current_instructeur , motivation , justificatif )
2019-07-11 11:37:59 +02:00
flash . notice = " Dossier traité avec succès. "
end
2019-12-19 13:25:32 +01:00
rescue AASM :: InvalidTransition = > e
flash . alert = aasm_error_message ( e , target_state : target_state )
2017-12-01 12:57:01 +01:00
end
2018-09-13 17:13:09 +02:00
render partial : 'state_button_refresh' , locals : { dossier : dossier }
2017-12-01 12:57:01 +01:00
end
2017-07-19 14:04:49 +02:00
def create_commentaire
2019-08-06 11:02:54 +02:00
@commentaire = CommentaireService . build ( current_instructeur , dossier , commentaire_params )
2017-10-30 17:21:05 +01:00
if @commentaire . save
2020-07-22 16:34:33 +02:00
@commentaire . dossier . update! ( last_commentaire_updated_at : Time . zone . now )
2021-04-07 19:09:43 +02:00
current_instructeur . follow ( dossier )
2017-10-30 17:21:05 +01:00
flash . notice = " Message envoyé "
2019-08-06 11:02:54 +02:00
redirect_to messagerie_instructeur_dossier_path ( procedure , dossier )
2017-10-30 17:21:05 +01:00
else
flash . alert = @commentaire . errors . full_messages
render :messagerie
end
2017-07-19 14:04:49 +02:00
end
2017-08-28 14:16:13 +02:00
def create_avis
2021-02-25 09:53:09 +01:00
@avis = create_avis_from_params ( dossier , current_instructeur )
2018-10-31 14:56:01 +01:00
if @avis . nil?
2019-08-06 11:02:54 +02:00
redirect_to avis_instructeur_dossier_path ( procedure , dossier )
2018-02-27 16:58:22 +01:00
else
2019-08-06 11:02:54 +02:00
@avis_seen_at = current_instructeur . follows . find_by ( dossier : dossier ) & . avis_seen_at
2018-02-27 16:58:22 +01:00
render :avis
end
2017-08-28 14:16:13 +02:00
end
2017-08-02 15:33:23 +02:00
def update_annotations
2019-08-06 11:02:54 +02:00
dossier = current_instructeur . dossiers . includes ( champs_private : :type_de_champ ) . find ( params [ :dossier_id ] )
2020-07-22 17:11:33 +02:00
dossier . assign_attributes ( champs_private_params )
if dossier . champs_private . any? ( & :changed? )
dossier . last_champ_private_updated_at = Time . zone . now
end
dossier . save
2020-07-28 14:22:56 +02:00
dossier . log_modifier_annotations! ( current_instructeur )
2019-08-06 11:02:54 +02:00
redirect_to annotations_privees_instructeur_dossier_path ( procedure , dossier )
2017-08-02 15:33:23 +02:00
end
2017-10-05 23:27:02 +02:00
def print
@dossier = dossier
render layout : " print "
end
2019-07-01 15:55:37 +02:00
def telecharger_pjs
2019-10-03 10:48:24 +02:00
return head ( :forbidden ) if ! dossier . attachments_downloadable?
2019-07-01 15:55:37 +02:00
files = ActiveStorage :: DownloadableFile . create_list_from_dossier ( dossier )
zipline ( files , " dossier- #{ dossier . id } .zip " )
end
2020-11-17 13:25:35 +01:00
def delete_dossier
if dossier . termine?
2020-11-26 15:13:32 +01:00
dossier . discard_and_keep_track! ( current_instructeur , :instructeur_request )
2020-11-17 13:25:35 +01:00
flash . notice = 'Le dossier a bien été supprimé'
redirect_to instructeur_procedure_path ( procedure )
else
flash . alert = " Suppression impossible : le dossier n'est pas terminé "
redirect_back ( fallback_location : instructeur_procedures_url )
end
end
2017-06-29 15:31:29 +02:00
private
def dossier
2021-04-16 17:17:11 +02:00
@dossier || = current_instructeur
. dossiers
. includes ( champs : :type_de_champ )
. find ( params [ :dossier_id ] )
2017-06-29 15:31:29 +02:00
end
2017-07-19 14:04:49 +02:00
def commentaire_params
2019-06-25 17:12:44 +02:00
params . require ( :commentaire ) . permit ( :body , :piece_jointe )
2017-07-19 14:04:49 +02:00
end
2017-08-28 14:16:13 +02:00
2017-08-02 15:33:23 +02:00
def champs_private_params
2018-04-03 17:53:14 +02:00
params . require ( :dossier ) . permit ( champs_private_attributes : [
2019-03-25 11:08:41 +01:00
:id , :primary_value , :secondary_value , :piece_justificative_file , :value , value : [ ] ,
champs_attributes : [ :id , :_destroy , :value , :primary_value , :secondary_value , :piece_justificative_file , value : [ ] ]
2018-04-03 17:53:14 +02:00
] )
2017-08-02 15:33:23 +02:00
end
2017-12-01 12:57:01 +01:00
2017-12-05 16:07:05 +01:00
def mark_demande_as_read
2019-08-06 11:02:54 +02:00
current_instructeur . mark_tab_as_seen ( dossier , :demande )
2017-12-05 16:07:05 +01:00
end
2017-12-05 17:20:10 +01:00
def mark_messagerie_as_read
2019-08-06 11:02:54 +02:00
current_instructeur . mark_tab_as_seen ( dossier , :messagerie )
2017-12-05 17:20:10 +01:00
end
2017-12-07 11:15:51 +01:00
def mark_avis_as_read
2019-08-06 11:02:54 +02:00
current_instructeur . mark_tab_as_seen ( dossier , :avis )
2017-12-07 11:15:51 +01:00
end
2017-12-07 14:37:34 +01:00
def mark_annotations_privees_as_read
2019-08-06 11:02:54 +02:00
current_instructeur . mark_tab_as_seen ( dossier , :annotations_privees )
2017-12-07 14:37:34 +01:00
end
2019-12-19 13:25:32 +01:00
def aasm_error_message ( exception , target_state : )
if exception . originating_state == target_state
" Le dossier est déjà #{ dossier_display_state ( target_state , lower : true ) } . "
else
" Le dossier est en ce moment #{ dossier_display_state ( exception . originating_state , lower : true ) } : il n’ est pas possible de le passer #{ dossier_display_state ( target_state , lower : true ) } . "
end
end
2017-06-29 15:31:29 +02:00
end
end