2017-06-29 15:30:42 +02:00
|
|
|
module NewGestionnaire
|
|
|
|
class ProceduresController < GestionnaireController
|
2017-07-03 14:05:55 +02:00
|
|
|
before_action :ensure_ownership!, except: [:index]
|
2017-09-07 16:04:53 +02:00
|
|
|
before_action :redirect_to_avis_if_needed, only: [:index]
|
2017-07-03 14:05:55 +02:00
|
|
|
|
2017-10-05 13:28:24 +02:00
|
|
|
ITEMS_PER_PAGE = 25
|
2017-09-27 15:16:07 +02:00
|
|
|
|
2017-07-03 14:05:55 +02:00
|
|
|
def index
|
2017-07-18 15:18:10 +02:00
|
|
|
@procedures = current_gestionnaire.procedures.order(archived_at: :desc, published_at: :desc)
|
2017-07-03 14:05:55 +02:00
|
|
|
|
2017-07-18 15:18:54 +02:00
|
|
|
dossiers = current_gestionnaire.dossiers
|
2017-07-17 11:22:12 +02:00
|
|
|
@dossiers_count_per_procedure = dossiers.all_state.group(:procedure_id).reorder(nil).count
|
|
|
|
@dossiers_a_suivre_count_per_procedure = dossiers.without_followers.en_cours.group(:procedure_id).reorder(nil).count
|
2017-07-03 14:05:55 +02:00
|
|
|
@dossiers_archived_count_per_procedure = dossiers.archived.group(:procedure_id).count
|
2017-07-17 11:22:12 +02:00
|
|
|
@dossiers_termines_count_per_procedure = dossiers.termine.group(:procedure_id).reorder(nil).count
|
2017-07-03 14:05:55 +02:00
|
|
|
|
2017-07-17 11:22:12 +02:00
|
|
|
@followed_dossiers_count_per_procedure = current_gestionnaire
|
|
|
|
.followed_dossiers
|
|
|
|
.en_cours
|
|
|
|
.where(procedure: @procedures)
|
|
|
|
.group(:procedure_id)
|
|
|
|
.reorder(nil)
|
|
|
|
.count
|
2017-07-03 14:05:55 +02:00
|
|
|
end
|
2017-06-29 15:30:42 +02:00
|
|
|
|
2017-07-11 16:09:03 +02:00
|
|
|
def show
|
|
|
|
@procedure = procedure
|
|
|
|
|
2017-09-28 11:04:18 +02:00
|
|
|
@current_filters = current_filters
|
|
|
|
@available_fields_to_filters = available_fields_to_filters
|
2017-10-02 17:03:38 +02:00
|
|
|
@displayed_fields = procedure_presentation.displayed_fields
|
|
|
|
@displayed_fields_values = displayed_fields_values
|
|
|
|
|
2017-07-11 16:09:03 +02:00
|
|
|
@a_suivre_dossiers = procedure
|
|
|
|
.dossiers
|
2017-07-31 18:00:43 +02:00
|
|
|
.includes(:user)
|
2017-07-11 16:09:03 +02:00
|
|
|
.without_followers
|
|
|
|
.en_cours
|
|
|
|
|
|
|
|
@followed_dossiers = current_gestionnaire
|
|
|
|
.followed_dossiers
|
2018-02-01 16:20:31 +01:00
|
|
|
.includes(:user)
|
2017-07-11 16:09:03 +02:00
|
|
|
.where(procedure: @procedure)
|
|
|
|
.en_cours
|
|
|
|
|
2017-07-31 18:35:34 +02:00
|
|
|
@followed_dossiers_id = current_gestionnaire
|
|
|
|
.followed_dossiers
|
|
|
|
.where(procedure: @procedure)
|
|
|
|
.pluck(:id)
|
|
|
|
|
2017-07-31 18:00:43 +02:00
|
|
|
@termines_dossiers = procedure.dossiers.includes(:user).termine
|
2017-07-11 16:09:03 +02:00
|
|
|
|
2017-07-31 18:00:43 +02:00
|
|
|
@all_state_dossiers = procedure.dossiers.includes(:user).all_state
|
2017-07-11 16:09:03 +02:00
|
|
|
|
2017-07-31 18:00:43 +02:00
|
|
|
@archived_dossiers = procedure.dossiers.includes(:user).archived
|
2017-07-11 15:40:09 +02:00
|
|
|
|
2017-09-28 19:07:18 +02:00
|
|
|
@dossiers = case statut
|
2017-07-11 15:40:09 +02:00
|
|
|
when 'a-suivre'
|
|
|
|
@a_suivre_dossiers
|
|
|
|
when 'suivis'
|
|
|
|
@followed_dossiers
|
|
|
|
when 'traites'
|
|
|
|
@termines_dossiers
|
|
|
|
when 'tous'
|
|
|
|
@all_state_dossiers
|
|
|
|
when 'archives'
|
|
|
|
@archived_dossiers
|
|
|
|
end
|
2017-09-21 16:47:43 +02:00
|
|
|
|
2017-09-27 15:16:07 +02:00
|
|
|
sorted_ids = sorted_ids(@dossiers)
|
|
|
|
|
2017-09-28 11:04:18 +02:00
|
|
|
if @current_filters.count > 0
|
|
|
|
filtered_ids = filtered_ids(@dossiers)
|
|
|
|
filtered_sorted_ids = sorted_ids.select { |id| filtered_ids.include?(id) }
|
|
|
|
else
|
|
|
|
filtered_sorted_ids = sorted_ids
|
|
|
|
end
|
|
|
|
|
2017-09-27 15:16:07 +02:00
|
|
|
page = params[:page].present? ? params[:page] : 1
|
|
|
|
|
2017-09-28 11:04:18 +02:00
|
|
|
filtered_sorted_paginated_ids = Kaminari
|
|
|
|
.paginate_array(filtered_sorted_ids)
|
2017-09-27 15:16:07 +02:00
|
|
|
.page(page)
|
|
|
|
.per(ITEMS_PER_PAGE)
|
|
|
|
|
2017-09-28 11:04:18 +02:00
|
|
|
@dossiers = @dossiers.where(id: filtered_sorted_paginated_ids)
|
2017-09-27 15:16:07 +02:00
|
|
|
|
2017-10-02 17:03:38 +02:00
|
|
|
eager_load_displayed_fields
|
|
|
|
|
2017-09-28 11:04:18 +02:00
|
|
|
@dossiers = @dossiers.sort_by { |d| filtered_sorted_paginated_ids.index(d.id) }
|
2017-09-27 15:16:07 +02:00
|
|
|
|
2017-09-28 11:04:18 +02:00
|
|
|
kaminarize(page, filtered_sorted_ids.count)
|
2017-07-11 16:09:03 +02:00
|
|
|
end
|
|
|
|
|
2017-10-02 17:03:38 +02:00
|
|
|
def update_displayed_fields
|
|
|
|
values = params[:values]
|
|
|
|
|
|
|
|
if values.nil?
|
|
|
|
values = []
|
|
|
|
end
|
|
|
|
|
|
|
|
fields = values.map do |value|
|
|
|
|
table, column = value.split("/")
|
|
|
|
|
|
|
|
c = procedure.fields.find do |field|
|
|
|
|
field['table'] == table && field['column'] == column
|
|
|
|
end
|
|
|
|
|
|
|
|
c.to_json
|
|
|
|
end
|
|
|
|
|
|
|
|
procedure_presentation.update_attributes(displayed_fields: fields)
|
|
|
|
|
2017-09-27 15:16:07 +02:00
|
|
|
current_sort = procedure_presentation.sort
|
|
|
|
if !values.include?("#{current_sort['table']}/#{current_sort['column']}")
|
|
|
|
procedure_presentation.update_attributes(sort: Procedure.default_sort)
|
|
|
|
end
|
|
|
|
|
2018-02-21 15:56:41 +01:00
|
|
|
redirect_back(fallback_location: gestionnaire_procedure_url(procedure))
|
2017-09-27 15:16:07 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_sort
|
|
|
|
current_sort = procedure_presentation.sort
|
|
|
|
table = params[:table]
|
|
|
|
column = params[:column]
|
|
|
|
|
|
|
|
if table == current_sort['table'] && column == current_sort['column']
|
|
|
|
order = current_sort['order'] == 'asc' ? 'desc' : 'asc'
|
|
|
|
else
|
|
|
|
order = 'asc'
|
|
|
|
end
|
|
|
|
|
|
|
|
sort = {
|
|
|
|
'table' => table,
|
|
|
|
'column' => column,
|
|
|
|
'order' => order
|
|
|
|
}.to_json
|
|
|
|
|
|
|
|
procedure_presentation.update_attributes(sort: sort)
|
|
|
|
|
2018-02-21 15:56:41 +01:00
|
|
|
redirect_back(fallback_location: gestionnaire_procedure_url(procedure))
|
2017-10-02 17:03:38 +02:00
|
|
|
end
|
|
|
|
|
2017-09-28 11:04:18 +02:00
|
|
|
def add_filter
|
2017-12-06 17:26:23 +01:00
|
|
|
if params[:value].present?
|
|
|
|
filters = procedure_presentation.filters
|
|
|
|
table, column = params[:field].split('/')
|
|
|
|
label = procedure.fields.find { |c| c['table'] == table && c['column'] == column }['label']
|
|
|
|
|
|
|
|
filters[statut] << {
|
|
|
|
'label' => label,
|
|
|
|
'table' => table,
|
|
|
|
'column' => column,
|
|
|
|
'value' => params[:value]
|
|
|
|
}
|
|
|
|
|
|
|
|
procedure_presentation.update_attributes(filters: filters.to_json)
|
|
|
|
end
|
2017-09-28 11:04:18 +02:00
|
|
|
|
2018-02-21 15:56:41 +01:00
|
|
|
redirect_back(fallback_location: gestionnaire_procedure_url(procedure))
|
2017-09-28 11:04:18 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def remove_filter
|
|
|
|
filters = procedure_presentation.filters
|
|
|
|
filter_to_remove = current_filters.find do |filter|
|
|
|
|
filter['table'] == params[:table] && filter['column'] == params[:column]
|
|
|
|
end
|
|
|
|
|
|
|
|
filters[statut] = filters[statut] - [filter_to_remove]
|
|
|
|
|
|
|
|
procedure_presentation.update_attributes(filters: filters.to_json)
|
|
|
|
|
2018-02-21 15:56:41 +01:00
|
|
|
redirect_back(fallback_location: gestionnaire_procedure_url(procedure))
|
2017-09-28 11:04:18 +02:00
|
|
|
end
|
|
|
|
|
2017-11-22 17:27:02 +01:00
|
|
|
def download_dossiers
|
|
|
|
export = procedure.generate_export
|
|
|
|
filename = "dossiers_#{procedure.procedure_path.path}_#{Time.now.strftime('%Y-%m-%d_%H-%M')}"
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.csv { send_data(SpreadsheetArchitect.to_csv(data: export[:data], headers: export[:headers]), filename: "#{filename}.csv") }
|
|
|
|
format.xlsx { send_data(SpreadsheetArchitect.to_xlsx(data: export[:data], headers: export[:headers]), filename: "#{filename}.xlsx") }
|
|
|
|
format.ods { send_data(SpreadsheetArchitect.to_ods(data: export[:data], headers: export[:headers]), filename: "#{filename}.ods") }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-29 15:30:42 +02:00
|
|
|
private
|
|
|
|
|
2017-09-28 19:07:18 +02:00
|
|
|
def statut
|
|
|
|
@statut ||= params[:statut].present? ? params[:statut] : 'a-suivre'
|
|
|
|
end
|
|
|
|
|
2017-06-29 15:30:42 +02:00
|
|
|
def procedure
|
|
|
|
Procedure.find(params[:procedure_id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_ownership!
|
|
|
|
if !procedure.gestionnaires.include?(current_gestionnaire)
|
|
|
|
flash[:alert] = "Vous n'avez pas accès à cette procédure"
|
|
|
|
redirect_to root_path
|
|
|
|
end
|
|
|
|
end
|
2017-09-07 16:04:53 +02:00
|
|
|
|
|
|
|
def redirect_to_avis_if_needed
|
|
|
|
if current_gestionnaire.procedures.count == 0 && current_gestionnaire.avis.count > 0
|
2018-02-21 15:45:21 +01:00
|
|
|
redirect_to gestionnaire_avis_index_path
|
2017-09-07 16:04:53 +02:00
|
|
|
end
|
|
|
|
end
|
2017-10-02 17:03:38 +02:00
|
|
|
|
|
|
|
def procedure_presentation
|
|
|
|
@procedure_presentation ||= current_gestionnaire.procedure_presentation_for_procedure_id(params[:procedure_id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def displayed_fields_values
|
|
|
|
procedure_presentation.displayed_fields.map do |field|
|
|
|
|
"#{field['table']}/#{field['column']}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-28 11:04:18 +02:00
|
|
|
def filtered_ids(dossiers)
|
|
|
|
current_filters.map do |filter|
|
|
|
|
case filter['table']
|
|
|
|
when 'self'
|
|
|
|
dossiers.where("? LIKE ?", filter['column'], "%#{filter['value']}%")
|
|
|
|
|
|
|
|
when 'france_connect_information'
|
|
|
|
dossiers
|
|
|
|
.includes(user: :france_connect_information)
|
|
|
|
.where("? LIKE ?", "france_connect_informations.#{filter['column']}", "%#{filter['value']}%")
|
|
|
|
|
|
|
|
when 'type_de_champ', 'type_de_champ_private'
|
|
|
|
relation = filter['table'] == 'type_de_champ' ? :champs : :champs_private
|
|
|
|
dossiers
|
|
|
|
.includes(relation)
|
|
|
|
.where("champs.type_de_champ_id = ?", filter['column'].to_i)
|
|
|
|
.where("champs.value LIKE ?", "%#{filter['value']}%")
|
|
|
|
|
|
|
|
when 'user', 'etablissement', 'entreprise'
|
2017-12-06 17:26:14 +01:00
|
|
|
if filter['column'] == 'date_creation'
|
|
|
|
dossiers
|
2018-01-15 19:34:08 +01:00
|
|
|
.includes(filter['table'])
|
|
|
|
.where("#{filter['table'].pluralize}.#{filter['column']} = ?", filter['value'].to_date)
|
2017-12-06 17:26:14 +01:00
|
|
|
else
|
|
|
|
dossiers
|
2018-01-15 19:34:08 +01:00
|
|
|
.includes(filter['table'])
|
|
|
|
.where("#{filter['table'].pluralize}.#{filter['column']} LIKE ?", "%#{filter['value']}%")
|
2017-12-06 17:26:14 +01:00
|
|
|
end
|
2017-09-28 11:04:18 +02:00
|
|
|
|
|
|
|
end.pluck(:id)
|
|
|
|
end.reduce(:&)
|
|
|
|
end
|
|
|
|
|
2017-09-27 15:16:07 +02:00
|
|
|
def sorted_ids(dossiers)
|
|
|
|
table = procedure_presentation.sort['table']
|
|
|
|
column = procedure_presentation.sort['column']
|
|
|
|
order = procedure_presentation.sort['order']
|
|
|
|
includes = ''
|
|
|
|
where = ''
|
|
|
|
|
2017-11-01 20:40:33 +01:00
|
|
|
sorted_ids = nil
|
|
|
|
|
2017-09-27 15:16:07 +02:00
|
|
|
case table
|
2017-11-01 20:40:33 +01:00
|
|
|
when 'notifications'
|
|
|
|
dossiers_id_with_notification = current_gestionnaire.notifications_for_procedure(procedure)
|
|
|
|
if order == 'desc'
|
|
|
|
sorted_ids = dossiers_id_with_notification + (dossiers.order('dossiers.updated_at desc').ids - dossiers_id_with_notification)
|
|
|
|
else
|
|
|
|
sorted_ids = (dossiers.order('dossiers.updated_at asc').ids - dossiers_id_with_notification) + dossiers_id_with_notification
|
|
|
|
end
|
2017-09-27 15:16:07 +02:00
|
|
|
when 'self'
|
|
|
|
order = "dossiers.#{column} #{order}"
|
2018-01-15 21:48:16 +01:00
|
|
|
when 'france_connect_information'
|
2017-09-27 15:16:07 +02:00
|
|
|
includes = { user: :france_connect_information }
|
|
|
|
order = "france_connect_informations.#{column} #{order}"
|
|
|
|
when 'type_de_champ', 'type_de_champ_private'
|
|
|
|
includes = table == 'type_de_champ' ? :champs : :champs_private
|
|
|
|
where = "champs.type_de_champ_id = #{column.to_i}"
|
|
|
|
order = "champs.value #{order}"
|
|
|
|
else
|
|
|
|
includes = table
|
|
|
|
order = "#{table.pluralize}.#{column} #{order}"
|
|
|
|
end
|
|
|
|
|
2017-11-01 20:40:33 +01:00
|
|
|
if sorted_ids.nil?
|
|
|
|
sorted_ids = dossiers.includes(includes).where(where).order(Dossier.sanitize_for_order(order)).pluck(:id)
|
|
|
|
end
|
|
|
|
|
|
|
|
sorted_ids
|
2017-09-27 15:16:07 +02:00
|
|
|
end
|
|
|
|
|
2017-09-28 11:04:18 +02:00
|
|
|
def current_filters
|
|
|
|
@current_filters ||= procedure_presentation.filters[statut]
|
|
|
|
end
|
|
|
|
|
|
|
|
def available_fields_to_filters
|
|
|
|
current_filters_fields_ids = current_filters.map do |field|
|
|
|
|
"#{field['table']}/#{field['column']}"
|
|
|
|
end
|
|
|
|
|
|
|
|
procedure.fields_for_select.reject do |field|
|
|
|
|
current_filters_fields_ids.include?(field[1])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-02 17:03:38 +02:00
|
|
|
def eager_load_displayed_fields
|
|
|
|
@displayed_fields
|
|
|
|
.reject { |field| field['table'] == 'self' }
|
|
|
|
.group_by do |field|
|
|
|
|
if ['type_de_champ', 'type_de_champ_private'].include?(field['table'])
|
|
|
|
'type_de_champ_group'
|
|
|
|
else
|
|
|
|
field['table']
|
|
|
|
end
|
2017-09-27 15:16:07 +02:00
|
|
|
end.each do |group_key, fields|
|
|
|
|
case group_key
|
2018-01-15 21:48:16 +01:00
|
|
|
when 'france_connect_information'
|
2017-09-27 15:16:07 +02:00
|
|
|
@dossiers = @dossiers.includes({ user: :france_connect_information })
|
|
|
|
when 'type_de_champ_group'
|
|
|
|
if fields.any? { |field| field['table'] == 'type_de_champ' }
|
2017-10-10 18:33:20 +02:00
|
|
|
@dossiers = @dossiers.includes(:champs).references(:champs)
|
2017-09-27 15:16:07 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if fields.any? { |field| field['table'] == 'type_de_champ_private' }
|
2017-10-10 18:33:20 +02:00
|
|
|
@dossiers = @dossiers.includes(:champs_private).references(:champs_private)
|
2017-09-27 15:16:07 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
where_conditions = fields.map do |field|
|
|
|
|
"champs.type_de_champ_id = #{field['column']}"
|
|
|
|
end.join(" OR ")
|
|
|
|
|
|
|
|
@dossiers = @dossiers.where(where_conditions)
|
|
|
|
else
|
|
|
|
@dossiers = @dossiers.includes(fields.first['table'])
|
2017-10-02 17:03:38 +02:00
|
|
|
end
|
2017-09-27 15:16:07 +02:00
|
|
|
end
|
|
|
|
end
|
2017-10-02 17:03:38 +02:00
|
|
|
|
2017-09-27 15:16:07 +02:00
|
|
|
def kaminarize(current_page, total)
|
|
|
|
@dossiers.instance_eval <<-EVAL
|
|
|
|
def current_page
|
|
|
|
#{current_page}
|
2017-10-02 17:03:38 +02:00
|
|
|
end
|
2017-09-27 15:16:07 +02:00
|
|
|
def total_pages
|
|
|
|
(#{total} / #{ITEMS_PER_PAGE}.to_f).ceil
|
|
|
|
end
|
2017-10-05 13:28:24 +02:00
|
|
|
def limit_value
|
2017-09-27 15:16:07 +02:00
|
|
|
#{ITEMS_PER_PAGE}
|
|
|
|
end
|
|
|
|
def first_page?
|
|
|
|
current_page == 1
|
|
|
|
end
|
|
|
|
def last_page?
|
|
|
|
current_page == total_pages
|
|
|
|
end
|
|
|
|
EVAL
|
2017-10-02 17:03:38 +02:00
|
|
|
end
|
2017-06-29 15:30:42 +02:00
|
|
|
end
|
|
|
|
end
|