2016-08-11 15:27:35 +02:00
|
|
|
class DossiersListGestionnaireService
|
2016-09-06 11:07:26 +02:00
|
|
|
def initialize current_devise_profil, liste, procedure = nil
|
2016-08-11 15:27:35 +02:00
|
|
|
@current_devise_profil = current_devise_profil
|
2017-01-09 12:26:41 +01:00
|
|
|
@liste = (DossiersListGestionnaireService.dossiers_liste_libelle.include?(liste) ? liste : 'all_state')
|
2016-09-06 11:07:26 +02:00
|
|
|
@procedure = procedure
|
2016-08-11 15:27:35 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def dossiers_to_display
|
2017-01-05 19:07:01 +01:00
|
|
|
@dossiers_to_display ||=
|
|
|
|
{'nouveaux' => nouveaux,
|
2017-09-21 16:18:20 +02:00
|
|
|
'a_traiter' => nouveaux,
|
2017-01-05 19:07:01 +01:00
|
|
|
'a_instruire' => a_instruire,
|
|
|
|
'termine' => termine,
|
2017-02-27 17:44:08 +01:00
|
|
|
'archive' => archive,
|
2017-01-05 19:07:01 +01:00
|
|
|
'all_state' => all_state}[@liste]
|
2016-08-11 15:27:35 +02:00
|
|
|
end
|
|
|
|
|
2016-10-11 18:05:49 +02:00
|
|
|
def self.dossiers_liste_libelle
|
2017-02-23 17:54:11 +01:00
|
|
|
['nouveaux', 'suivi', 'a_traiter', 'a_instruire', 'termine', 'all_state']
|
2016-10-24 16:00:03 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def all_state
|
2017-09-27 11:51:31 +02:00
|
|
|
@all_state ||= filter_dossiers.all_state.order_by_updated_at('asc')
|
2016-10-07 16:01:32 +02:00
|
|
|
end
|
|
|
|
|
2016-11-24 19:33:34 +01:00
|
|
|
def suivi
|
2017-05-12 17:58:25 +02:00
|
|
|
@suivi ||= @current_devise_profil.followed_dossiers.merge(dossiers_to_display)
|
2016-11-24 19:33:34 +01:00
|
|
|
end
|
|
|
|
|
2016-08-11 15:27:35 +02:00
|
|
|
def nouveaux
|
2017-12-05 16:14:02 +01:00
|
|
|
@nouveaux ||= filter_dossiers.en_construction.order_by_updated_at('asc')
|
2016-08-11 15:27:35 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def a_instruire
|
2017-09-27 11:51:31 +02:00
|
|
|
@a_instruire ||= filter_dossiers.en_instruction.order_by_updated_at('asc')
|
2016-08-11 15:27:35 +02:00
|
|
|
end
|
|
|
|
|
2017-02-27 17:44:08 +01:00
|
|
|
def archive
|
|
|
|
@archive ||= filter_dossiers.archived
|
|
|
|
end
|
|
|
|
|
2016-08-11 15:27:35 +02:00
|
|
|
def termine
|
2017-09-27 11:51:31 +02:00
|
|
|
@termine ||= filter_dossiers.termine.order_by_updated_at('asc')
|
2016-08-11 15:27:35 +02:00
|
|
|
end
|
|
|
|
|
2016-09-06 11:07:26 +02:00
|
|
|
def filter_dossiers
|
2016-10-04 19:19:03 +02:00
|
|
|
@filter_dossiers ||= @procedure.nil? ? @current_devise_profil.dossiers.joins(joins_filter).where(where_filter) : @procedure.dossiers.joins(joins_filter).where(where_filter)
|
2016-11-14 17:33:45 +01:00
|
|
|
@filter_dossiers.distinct
|
2016-08-11 15:27:35 +02:00
|
|
|
end
|
2016-09-27 17:26:49 +02:00
|
|
|
|
|
|
|
def filter_procedure_reset!
|
|
|
|
filter_procedure! nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def filter_procedure! procedure_id
|
|
|
|
@current_devise_profil.update_column :procedure_filter, procedure_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def default_sort
|
|
|
|
sort_preference = @current_devise_profil.preference_list_dossiers
|
|
|
|
.where(procedure: @procedure)
|
|
|
|
.where.not(order: nil).first
|
|
|
|
|
|
|
|
return {'nil' => 'nil'} if sort_preference.nil?
|
|
|
|
|
|
|
|
{
|
|
|
|
[sort_preference.table, sort_preference.attr]
|
|
|
|
.reject(&:nil?)
|
|
|
|
.join('.') => sort_preference.order
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-10-11 17:40:59 +02:00
|
|
|
def default_page
|
|
|
|
pref = current_preference_smart_listing_page
|
|
|
|
return pref.page if pref.procedure == @procedure && pref.liste == @liste
|
|
|
|
|
|
|
|
1
|
|
|
|
end
|
|
|
|
|
|
|
|
def change_page! new_page
|
|
|
|
pref = current_preference_smart_listing_page
|
|
|
|
|
2016-12-20 17:26:46 +01:00
|
|
|
if pref
|
2018-01-11 19:04:39 +01:00
|
|
|
if pref.liste != @liste || pref.procedure != @procedure
|
2016-12-20 17:26:46 +01:00
|
|
|
pref.liste = @liste
|
|
|
|
pref.procedure = @procedure
|
|
|
|
|
|
|
|
if new_page.nil?
|
|
|
|
pref.page = 1
|
|
|
|
pref.save
|
|
|
|
end
|
|
|
|
end
|
2016-10-11 17:40:59 +02:00
|
|
|
|
2018-01-11 19:04:39 +01:00
|
|
|
if new_page.present?
|
2016-12-20 17:26:46 +01:00
|
|
|
pref.page = new_page
|
2016-10-11 17:40:59 +02:00
|
|
|
pref.save
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-27 17:26:49 +02:00
|
|
|
def change_sort! new_sort
|
2016-10-04 19:19:03 +02:00
|
|
|
return if new_sort.blank?
|
2016-09-27 17:26:49 +02:00
|
|
|
|
|
|
|
raw_table_attr = new_sort.keys.first.split('.')
|
|
|
|
order = new_sort.values.first
|
|
|
|
|
|
|
|
table = (raw_table_attr.size == 2 ? raw_table_attr.first : nil)
|
|
|
|
attr = (raw_table_attr.size == 2 ? raw_table_attr.second : raw_table_attr.first)
|
|
|
|
|
|
|
|
reset_sort!
|
|
|
|
|
2016-10-14 12:02:39 +02:00
|
|
|
preference = @current_devise_profil.preference_list_dossiers
|
|
|
|
.find_by(table: table, attr: attr, procedure: @procedure)
|
|
|
|
|
2018-01-11 19:04:39 +01:00
|
|
|
preference.update order: order if preference.present?
|
2016-09-27 17:26:49 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def reset_sort!
|
|
|
|
@current_devise_profil.preference_list_dossiers
|
|
|
|
.where(procedure: @procedure)
|
|
|
|
.where.not(order: nil)
|
|
|
|
.update_all order: nil
|
|
|
|
end
|
2016-10-04 19:19:03 +02:00
|
|
|
|
|
|
|
def joins_filter
|
|
|
|
filter_preference_list.inject([]) do |acc, preference|
|
2018-01-11 19:04:39 +01:00
|
|
|
acc.push(preference.table.to_sym) if preference.table.present? && preference.filter.present?
|
2016-10-04 19:19:03 +02:00
|
|
|
acc
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def where_filter
|
|
|
|
filter_preference_list.inject('') do |acc, preference|
|
2018-01-11 19:04:39 +01:00
|
|
|
if preference.filter.present?
|
2017-11-20 18:55:59 +01:00
|
|
|
filter = preference.filter.tr('*', '%').gsub("'", "''")
|
2018-01-11 19:04:39 +01:00
|
|
|
filter = "%" + filter + "%" if !filter.include? '%'
|
2016-10-04 19:19:03 +02:00
|
|
|
|
2016-10-14 12:02:39 +02:00
|
|
|
value = preference.table_with_s_attr
|
|
|
|
|
|
|
|
if preference.table_attr.include?('champs')
|
|
|
|
value = 'champs.value'
|
|
|
|
|
|
|
|
acc += (acc.to_s.empty? ? ''.to_s : " AND ") +
|
|
|
|
'champs.type_de_champ_id = ' + preference.attr
|
|
|
|
end
|
|
|
|
|
2016-10-04 19:19:03 +02:00
|
|
|
acc += (acc.to_s.empty? ? ''.to_s : " AND ") +
|
2016-10-10 14:56:23 +02:00
|
|
|
"CAST(" +
|
2016-10-14 12:02:39 +02:00
|
|
|
value +
|
2016-10-10 14:56:23 +02:00
|
|
|
" as TEXT)" +
|
2016-10-04 19:19:03 +02:00
|
|
|
" LIKE " +
|
|
|
|
"'" +
|
|
|
|
filter +
|
|
|
|
"'"
|
|
|
|
end
|
|
|
|
acc
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_filter new_filter
|
|
|
|
raw_table_attr = new_filter.keys.first.split('.')
|
|
|
|
filter = new_filter.values.first
|
|
|
|
|
|
|
|
table = (raw_table_attr.size == 2 ? raw_table_attr.first : nil)
|
|
|
|
attr = (raw_table_attr.size == 2 ? raw_table_attr.second : raw_table_attr.first)
|
|
|
|
|
|
|
|
@current_devise_profil.preference_list_dossiers
|
|
|
|
.find_by(table: table, attr: attr, procedure: @procedure)
|
2016-10-14 12:02:39 +02:00
|
|
|
.update filter: filter.strip
|
2016-10-04 19:19:03 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def filter_preference_list
|
|
|
|
@filter_preference ||= @current_devise_profil.preference_list_dossiers
|
|
|
|
.where(procedure: @procedure)
|
|
|
|
.where.not(filter: nil)
|
|
|
|
.order(:id)
|
|
|
|
end
|
2016-10-11 17:40:59 +02:00
|
|
|
|
|
|
|
def current_preference_smart_listing_page
|
|
|
|
@current_devise_profil.preference_smart_listing_page
|
|
|
|
end
|
2016-11-14 14:39:59 +01:00
|
|
|
end
|