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
2023-03-01 18:30:10 +01:00
include TurboChampsConcern
2017-11-29 17:11:50 +01:00
2019-07-01 15:55:37 +02:00
include ActionController :: Streaming
include Zipline
2022-05-17 09:53:34 +02:00
before_action :redirect_on_dossier_not_found , only : :show
poc(batch_operation): quand un dossier est dans un batch, impossible de faire les actions : archive, unarchive, follow, unfollow, passer_en_instruction, repasser_en_construction, repasser_en_instruction, terminer, restore, destroy, extend_conservation
2022-11-25 10:24:00 +01:00
before_action :redirect_on_dossier_in_batch_operation , only : [ :archive , :unarchive , :follow , :unfollow , :passer_en_instruction , :repasser_en_construction , :repasser_en_instruction , :terminer , :restore , :destroy , :extend_conservation ]
2017-12-05 16:07:05 +01:00
after_action :mark_demande_as_read , only : :show
poc(batch_operation): quand un dossier est dans un batch, impossible de faire les actions : archive, unarchive, follow, unfollow, passer_en_instruction, repasser_en_construction, repasser_en_instruction, terminer, restore, destroy, extend_conservation
2022-11-25 10:24:00 +01:00
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
2021-12-03 16:09:51 +01:00
def extend_conservation
2022-04-08 13:16:08 +02:00
dossier . extend_conservation ( 1 . month )
2021-12-03 16:09:51 +01:00
flash [ :notice ] = t ( 'views.instructeurs.dossiers.archived_dossier' )
redirect_back ( fallback_location : instructeur_dossier_path ( @dossier . procedure , @dossier ) )
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
2022-02-11 08:45:16 +01:00
@attestation = dossier . attestation_template . render_attributes_for ( dossier : dossier )
2019-08-27 17:44:02 +02:00
2021-11-25 16:26:55 +01:00
render 'administrateurs/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
2022-03-11 00:17:36 +01:00
@demande_seen_at = current_instructeur . follows . find_by ( dossier : dossier_with_champs ) & . demande_seen_at
2022-12-01 12:15:29 +01:00
@is_dossier_in_batch_operation = dossier . batch_operation . present?
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
2023-01-17 16:33:48 +01:00
# instructeurs are scoped by groupe_instructeur to avoid enumeration
recipients = dossier . groupe_instructeur . instructeurs . where ( id : JSON . parse ( recipients ) )
2018-09-10 17:52:49 +02:00
2023-01-17 16:33:48 +01:00
if recipients . present?
recipients . each do | recipient |
recipient . follow ( dossier )
InstructeurMailer . send_dossier ( current_instructeur , dossier , recipient ) . deliver_later
end
flash . notice = " Dossier envoyé "
else
flash . alert = " Instructeur inconnu ou non présent sur la procédure "
2018-09-10 17:52:49 +02:00
end
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'
2022-03-11 13:55:36 +01:00
redirect_back ( fallback_location : instructeur_procedure_path ( procedure ) )
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 } "
2022-03-11 13:55:36 +01:00
redirect_back ( fallback_location : instructeur_procedure_path ( procedure ) )
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 )
2022-03-11 13:55:36 +01:00
redirect_back ( fallback_location : instructeur_procedure_path ( procedure ) )
2017-07-11 15:56:23 +02:00
end
def unarchive
2022-11-30 12:39:47 +01:00
dossier . desarchiver!
2022-03-11 13:55:36 +01:00
redirect_back ( fallback_location : instructeur_procedure_path ( procedure ) )
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
2021-11-26 12:19:40 +01:00
dossier . passer_en_instruction! ( instructeur : 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
2022-05-06 11:59:28 +02:00
@dossier = dossier
2023-02-24 15:48:29 +01:00
respond_to do | format |
format . turbo_stream do
render :change_state
end
format . html do
redirect_back ( fallback_location : instructeur_procedure_path ( procedure ) )
end
end
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
2022-12-04 12:34:34 +01:00
dossier . repasser_en_construction! ( instructeur : 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
2022-05-06 11:59:28 +02:00
@dossier = dossier
2023-02-24 15:48:29 +01:00
respond_to do | format |
format . turbo_stream do
render :change_state
end
format . html do
redirect_back ( fallback_location : instructeur_procedure_path ( procedure ) )
end
end
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. "
2021-11-26 12:19:40 +01:00
dossier . repasser_en_instruction! ( instructeur : 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
2022-05-06 11:59:28 +02:00
@dossier = dossier
2023-02-24 15:48:29 +01:00
respond_to do | format |
format . turbo_stream do
render :change_state
end
format . html do
redirect_back ( fallback_location : instructeur_procedure_path ( procedure ) )
end
end
2019-07-01 16:16:40 +02:00
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
2021-11-26 12:19:40 +01:00
h = { instructeur : current_instructeur , motivation : motivation , justificatif : justificatif }
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
2021-11-26 12:19:40 +01:00
dossier . refuser! ( h )
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
2021-11-26 12:19:40 +01:00
dossier . classer_sans_suite! ( h )
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
2021-11-26 12:19:40 +01:00
dossier . accepter! ( h )
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
2022-05-06 11:59:28 +02:00
@dossier = dossier
render :change_state
2017-12-01 12:57:01 +01:00
end
2017-07-19 14:04:49 +02:00
def create_commentaire
2021-11-30 18:56:12 +01:00
@commentaire = CommentaireService . create ( current_instructeur , dossier , commentaire_params )
2017-10-30 17:21:05 +01:00
2021-11-30 18:56:12 +01:00
if @commentaire . errors . empty?
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
2022-03-18 11:28:18 +01:00
dossier_with_champs . assign_attributes ( champs_private_params )
2022-11-29 11:30:06 +01:00
if dossier . champs_private_all . any? ( & :changed? )
2020-07-22 17:11:33 +02:00
dossier . last_champ_private_updated_at = Time . zone . now
end
2023-03-01 18:30:10 +01:00
if ! dossier . save ( context : :annotations )
flash . now . alert = dossier . errors . full_messages
end
2022-05-07 17:49:27 +02:00
respond_to do | format |
2023-03-01 18:30:10 +01:00
format . turbo_stream do
@to_show , @to_hide , @to_update = champs_to_turbo_update ( champs_private_params . fetch ( :champs_private_all_attributes ) , dossier . champs_private_all )
end
2022-05-07 17:49:27 +02:00
end
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
2023-01-30 18:38:54 +01:00
files = ActiveStorage :: DownloadableFile . create_list_from_dossiers ( Dossier . where ( id : dossier . id ) , with_champs_private : true , include_infos_administration : true )
2022-09-05 11:20:30 +02:00
cleaned_files = ActiveStorage :: DownloadableFile . cleanup_list_from_dossier ( files )
2019-07-01 15:55:37 +02:00
2022-09-05 11:20:30 +02:00
zipline ( cleaned_files , " dossier- #{ dossier . id } .zip " )
2019-07-01 15:55:37 +02:00
end
2022-03-11 13:57:09 +01:00
def destroy
2020-11-17 13:25:35 +01:00
if dossier . termine?
2022-03-09 10:27:43 +01:00
dossier . hide_and_keep_track! ( current_instructeur , :instructeur_request )
2021-12-06 11:00:29 +01:00
flash . notice = t ( 'instructeurs.dossiers.deleted_by_instructeur' )
2020-11-17 13:25:35 +01:00
else
2021-12-06 11:00:29 +01:00
flash . alert = t ( 'instructeurs.dossiers.impossible_deletion' )
2022-03-11 13:57:09 +01:00
end
redirect_back ( fallback_location : instructeur_procedure_path ( procedure ) )
end
def restore
dossier = current_instructeur . dossiers . find ( params [ :dossier_id ] )
dossier . restore ( current_instructeur )
flash . notice = t ( 'instructeurs.dossiers.restore' )
if dossier . termine?
redirect_to instructeur_procedure_path ( procedure , statut : :traites )
else
redirect_back ( fallback_location : instructeur_procedure_path ( procedure ) )
2020-11-17 13:25:35 +01:00
end
end
2017-06-29 15:31:29 +02:00
private
2022-05-07 17:49:27 +02:00
def dossier_scope
if action_name == 'update_annotations'
Dossier
. where ( id : current_instructeur . dossiers . visible_by_administration )
. or ( Dossier . where ( id : current_user . dossiers . for_procedure_preview ) )
else
current_instructeur . dossiers . visible_by_administration
end
end
2017-06-29 15:31:29 +02:00
def dossier
2023-01-09 14:21:25 +01:00
@dossier || = DossierPreloader . load_one ( dossier_scope . find ( params [ :dossier_id ] ) )
2022-03-11 00:17:36 +01:00
end
def dossier_with_champs
2023-01-10 13:55:48 +01:00
@dossier || = DossierPreloader . load_one ( dossier_scope . 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
2022-11-29 11:30:06 +01:00
champs_params = params . require ( :dossier ) . permit ( champs_private_attributes : [
2021-11-17 12:52:47 +01:00
:id , :primary_value , :secondary_value , :piece_justificative_file , :value_other , :external_id , :numero_allocataire , :code_postal , :departement , :code_departement , :value , value : [ ] ,
champs_attributes : [ :id , :_destroy , :value , :primary_value , :secondary_value , :piece_justificative_file , :value_other , :external_id , :numero_allocataire , :code_postal , :departement , :code_departement , value : [ ] ]
2018-04-03 17:53:14 +02:00
] )
2022-11-29 11:30:06 +01:00
champs_params [ :champs_private_all_attributes ] = champs_params . delete ( :champs_private_attributes ) || { }
champs_params
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
2022-03-18 11:28:18 +01: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
2022-03-18 11:28:18 +01: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
2022-03-18 11:28:18 +01: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
2022-03-18 11:28:18 +01: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 ) } . "
2023-01-18 18:31:37 +01:00
elsif exception . failures . include? ( :can_terminer? ) && dossier . any_etablissement_as_degraded_mode?
2022-09-21 15:05:54 +02:00
" Les données relatives au SIRET de ce dossier n’ ont pas pu encore être vérifiées : il n’ est pas possible de le passer #{ dossier_display_state ( target_state , lower : true ) } . "
2019-12-19 13:25:32 +01:00
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
2022-05-17 09:53:34 +02:00
def redirect_on_dossier_not_found
2022-05-07 17:49:27 +02:00
if ! dossier_scope . exists? ( id : params [ :dossier_id ] )
2022-05-17 09:53:34 +02:00
redirect_to instructeur_procedure_path ( procedure )
end
end
poc(batch_operation): quand un dossier est dans un batch, impossible de faire les actions : archive, unarchive, follow, unfollow, passer_en_instruction, repasser_en_construction, repasser_en_instruction, terminer, restore, destroy, extend_conservation
2022-11-25 10:24:00 +01:00
def redirect_on_dossier_in_batch_operation
dossier_in_batch = begin
dossier
rescue ActiveRecord :: RecordNotFound
current_instructeur . dossiers . find ( params [ :dossier_id ] )
end
if dossier_in_batch . batch_operation . present?
2022-12-01 12:15:29 +01:00
flash . alert = " Votre action n'a pas été effectuée, ce dossier fait parti d'un traitement de masse. "
poc(batch_operation): quand un dossier est dans un batch, impossible de faire les actions : archive, unarchive, follow, unfollow, passer_en_instruction, repasser_en_construction, repasser_en_instruction, terminer, restore, destroy, extend_conservation
2022-11-25 10:24:00 +01:00
redirect_back ( fallback_location : instructeur_dossier_path ( procedure , dossier_in_batch ) )
end
end
2017-06-29 15:31:29 +02:00
end
end