2015-09-22 10:15:12 +02:00
|
|
|
class Gestionnaire < ActiveRecord::Base
|
|
|
|
devise :database_authenticatable, :registerable,
|
2017-06-12 13:49:51 +02:00
|
|
|
:recoverable, :rememberable, :trackable, :validatable
|
2015-11-10 10:23:15 +01:00
|
|
|
|
2016-05-20 15:39:17 +02:00
|
|
|
has_and_belongs_to_many :administrateurs
|
2015-11-16 18:08:19 +01:00
|
|
|
|
2016-10-11 15:37:16 +02:00
|
|
|
has_one :preference_smart_listing_page, dependent: :destroy
|
|
|
|
|
2016-06-20 17:37:04 +02:00
|
|
|
has_many :assign_to, dependent: :destroy
|
2017-07-18 15:26:33 +02:00
|
|
|
has_many :procedures, -> { publiees_ou_archivees }, through: :assign_to
|
2017-07-10 16:11:12 +02:00
|
|
|
has_many :dossiers, -> { state_not_brouillon }, through: :procedures
|
2017-05-12 17:58:25 +02:00
|
|
|
has_many :followed_dossiers, through: :follows, source: :dossier
|
2016-07-18 18:24:29 +02:00
|
|
|
has_many :follows
|
2016-08-01 18:10:32 +02:00
|
|
|
has_many :preference_list_dossiers
|
2017-04-25 12:09:11 +02:00
|
|
|
has_many :avis
|
2016-03-14 16:49:12 +01:00
|
|
|
|
2016-08-02 14:49:41 +02:00
|
|
|
after_create :build_default_preferences_list_dossier
|
2016-10-11 15:37:16 +02:00
|
|
|
after_create :build_default_preferences_smart_listing_page
|
2017-02-07 16:56:21 +01:00
|
|
|
|
|
|
|
include CredentialsSyncableConcern
|
2016-08-02 14:49:41 +02:00
|
|
|
|
2017-01-05 12:44:15 +01:00
|
|
|
def procedure_filter
|
2017-06-29 11:45:30 +02:00
|
|
|
procedure_id = self[:procedure_filter]
|
|
|
|
if procedures.find_by(id: procedure_id).present?
|
|
|
|
procedure_id
|
|
|
|
else
|
|
|
|
self.update_column(:procedure_filter, nil)
|
|
|
|
nil
|
|
|
|
end
|
2017-01-05 12:44:15 +01:00
|
|
|
end
|
|
|
|
|
2017-05-16 15:20:38 +02:00
|
|
|
def can_view_dossier?(dossier_id)
|
|
|
|
avis.where(dossier_id: dossier_id).any? ||
|
|
|
|
dossiers.where(id: dossier_id).any?
|
|
|
|
end
|
|
|
|
|
2017-07-17 11:28:14 +02:00
|
|
|
def follow(dossier)
|
|
|
|
return if follow?(dossier)
|
|
|
|
|
|
|
|
followed_dossiers << dossier
|
|
|
|
end
|
|
|
|
|
2017-07-17 13:13:20 +02:00
|
|
|
def follow?(dossier)
|
|
|
|
followed_dossiers.include?(dossier)
|
2016-07-18 18:24:29 +02:00
|
|
|
end
|
2016-08-02 14:49:41 +02:00
|
|
|
|
2017-04-27 16:29:04 +02:00
|
|
|
def assigned_on_procedure?(procedure_id)
|
|
|
|
procedures.find_by(id: procedure_id).present?
|
|
|
|
end
|
|
|
|
|
2016-10-07 12:10:58 +02:00
|
|
|
def build_default_preferences_list_dossier procedure_id=nil
|
|
|
|
PreferenceListDossier.available_columns_for(procedure_id).each do |table|
|
2016-08-02 14:49:41 +02:00
|
|
|
table.second.each do |column|
|
|
|
|
if valid_couple_table_attr? table.first, column.first
|
|
|
|
PreferenceListDossier.create(
|
|
|
|
libelle: column.second[:libelle],
|
|
|
|
table: column.second[:table],
|
|
|
|
attr: column.second[:attr],
|
|
|
|
attr_decorate: column.second[:attr_decorate],
|
|
|
|
bootstrap_lg: column.second[:bootstrap_lg],
|
|
|
|
order: nil,
|
|
|
|
filter: nil,
|
2016-10-07 12:10:58 +02:00
|
|
|
procedure_id: procedure_id,
|
2016-08-02 14:49:41 +02:00
|
|
|
gestionnaire: self
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-10-11 15:37:16 +02:00
|
|
|
def build_default_preferences_smart_listing_page
|
|
|
|
PreferenceSmartListingPage.create(page: 1, procedure: nil, gestionnaire: self, liste: 'a_traiter')
|
|
|
|
end
|
|
|
|
|
2016-12-27 10:57:21 +01:00
|
|
|
def notifications
|
2017-01-05 12:44:15 +01:00
|
|
|
Notification.where(already_read: false, dossier_id: follows.pluck(:dossier_id)).order("updated_at DESC")
|
2016-12-27 10:57:21 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def notifications_for procedure
|
2017-05-12 17:58:25 +02:00
|
|
|
procedure_ids = followed_dossiers.pluck(:procedure_id)
|
2016-12-26 17:25:59 +01:00
|
|
|
|
|
|
|
if procedure_ids.include?(procedure.id)
|
2017-05-12 17:58:25 +02:00
|
|
|
return followed_dossiers.where(procedure_id: procedure.id)
|
2016-12-26 17:25:59 +01:00
|
|
|
.inject(0) do |acc, dossier|
|
|
|
|
acc += dossier.notifications.where(already_read: false).count
|
|
|
|
end
|
|
|
|
end
|
|
|
|
0
|
|
|
|
end
|
|
|
|
|
2017-05-12 11:35:01 +02:00
|
|
|
def dossiers_with_notifications_count_for_procedure(procedure)
|
2017-05-12 17:58:25 +02:00
|
|
|
followed_dossiers_id = followed_dossiers.where(procedure: procedure).pluck(:id)
|
2017-05-12 12:12:57 +02:00
|
|
|
Notification.unread.where(dossier_id: followed_dossiers_id).select(:dossier_id).distinct(:dossier_id).count
|
2017-01-02 18:27:01 +01:00
|
|
|
end
|
|
|
|
|
2017-08-30 11:12:58 +02:00
|
|
|
def notifications_count_per_procedure
|
|
|
|
followed_dossiers
|
|
|
|
.joins(:notifications)
|
|
|
|
.where(notifications: { already_read: false })
|
|
|
|
.group('procedure_id')
|
|
|
|
.count
|
|
|
|
end
|
|
|
|
|
2017-04-26 11:28:58 +02:00
|
|
|
def dossiers_with_notifications_count
|
|
|
|
notifications.pluck(:dossier_id).uniq.count
|
|
|
|
end
|
|
|
|
|
2017-05-12 16:47:18 +02:00
|
|
|
def last_week_overview
|
|
|
|
start_date = DateTime.now.beginning_of_week
|
|
|
|
|
|
|
|
active_procedure_overviews = procedures
|
2017-07-11 15:52:06 +02:00
|
|
|
.publiees
|
2017-06-27 18:00:05 +02:00
|
|
|
.map { |procedure| procedure.procedure_overview(start_date) }
|
2017-05-12 16:47:18 +02:00
|
|
|
.select(&:had_some_activities?)
|
|
|
|
|
2017-06-27 18:00:05 +02:00
|
|
|
if active_procedure_overviews.count == 0
|
2017-05-12 16:47:18 +02:00
|
|
|
nil
|
|
|
|
else
|
|
|
|
{
|
|
|
|
start_date: start_date,
|
|
|
|
procedure_overviews: active_procedure_overviews,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-02 17:03:38 +02:00
|
|
|
def procedure_presentation_for_procedure_id(procedure_id)
|
|
|
|
assign_to.find_by(procedure_id: procedure_id).procedure_presentation_or_default
|
|
|
|
end
|
|
|
|
|
2016-08-02 14:49:41 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
def valid_couple_table_attr? table, column
|
|
|
|
couples = [{
|
|
|
|
table: :dossier,
|
|
|
|
column: :dossier_id
|
|
|
|
}, {
|
|
|
|
table: :procedure,
|
|
|
|
column: :libelle
|
|
|
|
}, {
|
|
|
|
table: :etablissement,
|
|
|
|
column: :siret
|
|
|
|
}, {
|
|
|
|
table: :entreprise,
|
|
|
|
column: :raison_sociale
|
|
|
|
}, {
|
|
|
|
table: :dossier,
|
|
|
|
column: :state
|
|
|
|
}]
|
|
|
|
|
|
|
|
couples.include?({table: table, column: column})
|
|
|
|
end
|
2015-09-22 10:15:12 +02:00
|
|
|
end
|