2018-11-08 18:09:27 +01:00
|
|
|
|
require Rails.root.join('lib', 'percentile')
|
|
|
|
|
|
2018-03-06 13:44:29 +01:00
|
|
|
|
class Procedure < ApplicationRecord
|
2019-09-17 10:16:52 +02:00
|
|
|
|
self.ignored_columns = ['logo', 'logo_secure_token']
|
2019-08-28 16:14:20 +02:00
|
|
|
|
|
2019-09-17 15:52:38 +02:00
|
|
|
|
include ProcedureStatsConcern
|
|
|
|
|
|
2018-05-24 23:29:33 +02:00
|
|
|
|
MAX_DUREE_CONSERVATION = 36
|
2019-10-21 17:14:56 +02:00
|
|
|
|
MAX_DUREE_CONSERVATION_EXPORT = 3.hours
|
2018-05-24 23:29:33 +02:00
|
|
|
|
|
2019-06-12 19:10:53 +02:00
|
|
|
|
has_many :types_de_champ, -> { root.public_only.ordered }, inverse_of: :procedure, dependent: :destroy
|
|
|
|
|
has_many :types_de_champ_private, -> { root.private_only.ordered }, class_name: 'TypeDeChamp', inverse_of: :procedure, dependent: :destroy
|
2018-05-30 11:36:48 +02:00
|
|
|
|
has_many :deleted_dossiers, dependent: :destroy
|
2017-03-07 10:25:28 +01:00
|
|
|
|
|
2016-01-18 16:20:51 +01:00
|
|
|
|
has_one :module_api_carto, dependent: :destroy
|
2017-06-08 14:16:48 +02:00
|
|
|
|
has_one :attestation_template, dependent: :destroy
|
2016-01-18 16:20:51 +01:00
|
|
|
|
|
2018-04-24 15:23:07 +02:00
|
|
|
|
belongs_to :parent_procedure, class_name: 'Procedure'
|
2018-04-17 16:11:49 +02:00
|
|
|
|
belongs_to :service
|
2016-01-18 16:20:51 +01:00
|
|
|
|
|
2018-03-23 11:39:36 +01:00
|
|
|
|
has_many :administrateurs_procedures
|
2019-04-29 18:27:44 +02:00
|
|
|
|
has_many :administrateurs, through: :administrateurs_procedures, after_remove: -> (procedure, _admin) { procedure.validate! }
|
2019-08-19 16:12:30 +02:00
|
|
|
|
has_many :groupe_instructeurs, dependent: :destroy
|
2016-05-20 15:39:17 +02:00
|
|
|
|
|
2019-08-22 17:58:31 +02:00
|
|
|
|
has_many :dossiers, through: :groupe_instructeurs, dependent: :restrict_with_exception
|
|
|
|
|
|
2017-05-26 21:55:19 +02:00
|
|
|
|
has_one :initiated_mail, class_name: "Mails::InitiatedMail", dependent: :destroy
|
|
|
|
|
has_one :received_mail, class_name: "Mails::ReceivedMail", dependent: :destroy
|
|
|
|
|
has_one :closed_mail, class_name: "Mails::ClosedMail", dependent: :destroy
|
|
|
|
|
has_one :refused_mail, class_name: "Mails::RefusedMail", dependent: :destroy
|
|
|
|
|
has_one :without_continuation_mail, class_name: "Mails::WithoutContinuationMail", dependent: :destroy
|
|
|
|
|
|
2019-10-15 09:28:21 +02:00
|
|
|
|
has_one :defaut_groupe_instructeur, -> { order(:id) }, class_name: 'GroupeInstructeur', inverse_of: :procedure
|
2019-09-16 15:37:12 +02:00
|
|
|
|
|
2019-08-28 13:11:58 +02:00
|
|
|
|
has_one_attached :logo
|
2018-04-11 14:35:52 +02:00
|
|
|
|
has_one_attached :notice
|
2018-05-31 10:59:38 +02:00
|
|
|
|
has_one_attached :deliberation
|
2019-10-03 15:35:31 +02:00
|
|
|
|
|
|
|
|
|
has_one_attached :csv_export_file
|
|
|
|
|
has_one_attached :xlsx_export_file
|
|
|
|
|
has_one_attached :ods_export_file
|
2018-04-11 14:35:52 +02:00
|
|
|
|
|
2018-11-14 16:24:34 +01:00
|
|
|
|
accepts_nested_attributes_for :types_de_champ, reject_if: proc { |attributes| attributes['libelle'].blank? }, allow_destroy: true
|
|
|
|
|
accepts_nested_attributes_for :types_de_champ_private, reject_if: proc { |attributes| attributes['libelle'].blank? }, allow_destroy: true
|
2015-11-10 10:23:15 +01:00
|
|
|
|
|
2017-06-27 14:22:43 +02:00
|
|
|
|
default_scope { where(hidden_at: nil) }
|
2018-05-16 17:21:12 +02:00
|
|
|
|
scope :brouillons, -> { where(aasm_state: :brouillon) }
|
|
|
|
|
scope :publiees, -> { where(aasm_state: :publiee) }
|
|
|
|
|
scope :archivees, -> { where(aasm_state: :archivee) }
|
|
|
|
|
scope :publiees_ou_archivees, -> { where(aasm_state: [:publiee, :archivee]) }
|
2017-07-17 15:06:36 +02:00
|
|
|
|
scope :by_libelle, -> { order(libelle: :asc) }
|
2018-04-12 18:36:09 +02:00
|
|
|
|
scope :created_during, -> (range) { where(created_at: range) }
|
|
|
|
|
scope :cloned_from_library, -> { where(cloned_from_library: true) }
|
2019-05-23 14:28:14 +02:00
|
|
|
|
scope :declarative, -> { where.not(declarative_with_state: nil) }
|
2017-05-26 21:30:11 +02:00
|
|
|
|
|
2018-11-01 14:04:32 +01:00
|
|
|
|
scope :for_api, -> {
|
|
|
|
|
includes(
|
2019-02-26 16:18:04 +01:00
|
|
|
|
:administrateurs,
|
2018-11-01 14:04:32 +01:00
|
|
|
|
:types_de_champ_private,
|
|
|
|
|
:types_de_champ,
|
|
|
|
|
:module_api_carto
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-23 14:28:14 +02:00
|
|
|
|
enum declarative_with_state: {
|
|
|
|
|
en_instruction: 'en_instruction',
|
|
|
|
|
accepte: 'accepte'
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-19 20:55:24 +01:00
|
|
|
|
scope :for_api_v2, -> {
|
|
|
|
|
includes(administrateurs: :user)
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-22 11:21:52 +02:00
|
|
|
|
validates :libelle, presence: true, allow_blank: false, allow_nil: false
|
|
|
|
|
validates :description, presence: true, allow_blank: false, allow_nil: false
|
2019-04-29 18:27:44 +02:00
|
|
|
|
validates :administrateurs, presence: true
|
2019-07-29 16:01:13 +02:00
|
|
|
|
validates :lien_site_web, presence: true, if: :publiee?
|
2019-07-30 16:54:43 +02:00
|
|
|
|
validate :validate_for_publication, on: :publication
|
2018-05-31 10:59:38 +02:00
|
|
|
|
validate :check_juridique
|
2019-09-17 15:55:35 +02:00
|
|
|
|
validates :path, presence: true, format: { with: /\A[a-z0-9_\-]{3,50}\z/ }, uniqueness: { scope: [:path, :archived_at, :hidden_at], case_sensitive: false }
|
2018-05-24 23:29:33 +02:00
|
|
|
|
# FIXME: remove duree_conservation_required flag once all procedures are converted to the new style
|
|
|
|
|
validates :duree_conservation_dossiers_dans_ds, allow_nil: false, numericality: { only_integer: true, greater_than_or_equal_to: 1, less_than_or_equal_to: MAX_DUREE_CONSERVATION }, if: :durees_conservation_required
|
|
|
|
|
validates :duree_conservation_dossiers_hors_ds, allow_nil: false, numericality: { only_integer: true, greater_than_or_equal_to: 0 }, if: :durees_conservation_required
|
|
|
|
|
validates :duree_conservation_dossiers_dans_ds, allow_nil: true, numericality: { only_integer: true, greater_than_or_equal_to: 1, less_than_or_equal_to: MAX_DUREE_CONSERVATION }, unless: :durees_conservation_required
|
|
|
|
|
validates :duree_conservation_dossiers_hors_ds, allow_nil: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 }, unless: :durees_conservation_required
|
2019-07-17 17:13:08 +02:00
|
|
|
|
validates_with MonAvisEmbedValidator
|
2018-06-01 11:06:12 +02:00
|
|
|
|
before_save :update_juridique_required
|
2018-05-25 00:16:13 +02:00
|
|
|
|
before_save :update_durees_conservation_required
|
2019-09-16 17:00:37 +02:00
|
|
|
|
after_initialize :ensure_path_exists
|
|
|
|
|
before_save :ensure_path_exists
|
2019-08-27 15:28:02 +02:00
|
|
|
|
after_create :ensure_default_groupe_instructeur
|
2018-06-01 11:06:12 +02:00
|
|
|
|
|
2018-05-17 15:38:49 +02:00
|
|
|
|
include AASM
|
|
|
|
|
|
|
|
|
|
aasm whiny_persistence: true do
|
|
|
|
|
state :brouillon, initial: true
|
|
|
|
|
state :publiee
|
|
|
|
|
state :archivee
|
|
|
|
|
state :hidden
|
|
|
|
|
|
2019-09-16 17:00:37 +02:00
|
|
|
|
event :publish, before: :before_publish, after: :after_publish do
|
2018-05-17 15:38:49 +02:00
|
|
|
|
transitions from: :brouillon, to: :publiee
|
|
|
|
|
transitions from: :archivee, to: :publiee
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
event :archive, after: :after_archive do
|
|
|
|
|
transitions from: :publiee, to: :archivee
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
event :hide, after: :after_hide do
|
|
|
|
|
transitions from: :brouillon, to: :hidden
|
|
|
|
|
transitions from: :publiee, to: :hidden
|
|
|
|
|
transitions from: :archivee, to: :hidden
|
|
|
|
|
end
|
2018-10-09 12:11:13 +02:00
|
|
|
|
|
|
|
|
|
event :draft, after: :after_draft do
|
|
|
|
|
transitions from: :publiee, to: :brouillon
|
|
|
|
|
end
|
2018-05-17 15:38:49 +02:00
|
|
|
|
end
|
2018-05-17 15:39:37 +02:00
|
|
|
|
|
2019-07-30 16:54:43 +02:00
|
|
|
|
def publish_or_reopen!(administrateur)
|
2019-09-16 17:00:37 +02:00
|
|
|
|
Procedure.transaction do
|
|
|
|
|
if brouillon?
|
|
|
|
|
reset!
|
|
|
|
|
end
|
2019-07-30 16:54:43 +02:00
|
|
|
|
|
|
|
|
|
other_procedure = other_procedure_with_path(path)
|
|
|
|
|
if other_procedure.present? && administrateur.owns?(other_procedure)
|
|
|
|
|
other_procedure.archive!
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
publish!
|
2018-09-07 18:42:12 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-10-03 15:35:31 +02:00
|
|
|
|
def csv_export_stale?
|
2019-10-21 17:14:56 +02:00
|
|
|
|
!csv_export_file.attached? || csv_export_file.created_at < MAX_DUREE_CONSERVATION_EXPORT.ago
|
2019-10-03 15:35:31 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def xlsx_export_stale?
|
2019-10-21 17:14:56 +02:00
|
|
|
|
!xlsx_export_file.attached? || xlsx_export_file.created_at < MAX_DUREE_CONSERVATION_EXPORT.ago
|
2019-10-03 15:35:31 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def ods_export_stale?
|
2019-10-21 17:14:56 +02:00
|
|
|
|
!ods_export_file.attached? || ods_export_file.created_at < MAX_DUREE_CONSERVATION_EXPORT.ago
|
2019-10-03 15:35:31 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-10-24 15:50:43 +02:00
|
|
|
|
def export_queued?(format)
|
|
|
|
|
case format.to_sym
|
|
|
|
|
when :csv
|
|
|
|
|
return csv_export_queued?
|
|
|
|
|
when :xlsx
|
|
|
|
|
return xlsx_export_queued?
|
|
|
|
|
when :ods
|
|
|
|
|
return ods_export_queued?
|
|
|
|
|
end
|
|
|
|
|
false
|
|
|
|
|
end
|
|
|
|
|
|
2019-10-03 15:35:31 +02:00
|
|
|
|
def should_generate_export?(format)
|
|
|
|
|
case format.to_sym
|
|
|
|
|
when :csv
|
|
|
|
|
return csv_export_stale? && !csv_export_queued?
|
|
|
|
|
when :xlsx
|
|
|
|
|
return xlsx_export_stale? && !xlsx_export_queued?
|
|
|
|
|
when :ods
|
|
|
|
|
return ods_export_stale? && !ods_export_queued?
|
|
|
|
|
end
|
|
|
|
|
false
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def export_file(export_format)
|
|
|
|
|
case export_format.to_sym
|
|
|
|
|
when :csv
|
|
|
|
|
csv_export_file
|
|
|
|
|
when :xlsx
|
|
|
|
|
xlsx_export_file
|
|
|
|
|
when :ods
|
|
|
|
|
ods_export_file
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def queue_export(instructeur, export_format)
|
|
|
|
|
case export_format.to_sym
|
|
|
|
|
when :csv
|
|
|
|
|
update(csv_export_queued: true)
|
|
|
|
|
when :xlsx
|
|
|
|
|
update(xlsx_export_queued: true)
|
|
|
|
|
when :ods
|
|
|
|
|
update(ods_export_queued: true)
|
|
|
|
|
end
|
2019-10-24 15:30:40 +02:00
|
|
|
|
ExportProcedureJob.perform_later(self, instructeur, export_format)
|
2019-10-03 15:35:31 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def prepare_export_download(format)
|
|
|
|
|
service = ProcedureExportV2Service.new(self, self.dossiers)
|
|
|
|
|
filename = export_filename(format)
|
|
|
|
|
|
|
|
|
|
case format.to_sym
|
|
|
|
|
when :csv
|
|
|
|
|
csv_export_file.attach(
|
|
|
|
|
io: StringIO.new(service.to_csv),
|
|
|
|
|
filename: filename,
|
|
|
|
|
content_type: 'text/csv'
|
|
|
|
|
)
|
|
|
|
|
update(csv_export_queued: false)
|
|
|
|
|
when :xlsx
|
|
|
|
|
xlsx_export_file.attach(
|
|
|
|
|
io: StringIO.new(service.to_xlsx),
|
|
|
|
|
filename: filename,
|
|
|
|
|
content_type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
|
|
|
|
|
)
|
|
|
|
|
update(xlsx_export_queued: false)
|
|
|
|
|
when :ods
|
|
|
|
|
ods_export_file.attach(
|
|
|
|
|
io: StringIO.new(service.to_ods),
|
|
|
|
|
filename: filename,
|
|
|
|
|
content_type: 'application/vnd.oasis.opendocument.spreadsheet'
|
|
|
|
|
)
|
|
|
|
|
update(ods_export_queued: false)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-08-13 17:49:15 +02:00
|
|
|
|
def reset!
|
|
|
|
|
if locked?
|
|
|
|
|
raise "Can not reset a locked procedure."
|
|
|
|
|
else
|
2019-08-22 17:58:31 +02:00
|
|
|
|
groupe_instructeurs.each { |gi| gi.dossiers.destroy_all }
|
2019-10-03 15:35:31 +02:00
|
|
|
|
purge_export_files
|
2018-08-13 17:49:15 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-07-30 16:54:43 +02:00
|
|
|
|
def validate_for_publication
|
|
|
|
|
old_attributes = self.slice(:aasm_state, :archived_at)
|
|
|
|
|
self.aasm_state = :publiee
|
|
|
|
|
self.archived_at = nil
|
|
|
|
|
|
|
|
|
|
is_valid = validate
|
|
|
|
|
|
|
|
|
|
self.attributes = old_attributes
|
|
|
|
|
|
|
|
|
|
is_valid
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def suggested_path(administrateur)
|
|
|
|
|
if path_customized?
|
|
|
|
|
return path
|
|
|
|
|
end
|
|
|
|
|
slug = libelle&.parameterize&.first(50)
|
|
|
|
|
suggestion = slug
|
|
|
|
|
counter = 1
|
|
|
|
|
while !path_available?(administrateur, suggestion)
|
|
|
|
|
counter = counter + 1
|
|
|
|
|
suggestion = "#{slug}-#{counter}"
|
|
|
|
|
end
|
|
|
|
|
suggestion
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def other_procedure_with_path(path)
|
|
|
|
|
Procedure.publiees
|
|
|
|
|
.where.not(id: self.id)
|
|
|
|
|
.find_by(path: path)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def path_available?(administrateur, path)
|
|
|
|
|
procedure = other_procedure_with_path(path)
|
|
|
|
|
|
|
|
|
|
procedure.blank? || administrateur.owns?(procedure)
|
|
|
|
|
end
|
|
|
|
|
|
2019-10-03 15:35:31 +02:00
|
|
|
|
def purge_export_files
|
|
|
|
|
xlsx_export_file.purge_later
|
|
|
|
|
ods_export_file.purge_later
|
|
|
|
|
csv_export_file.purge_later
|
2019-10-16 11:40:19 +02:00
|
|
|
|
|
|
|
|
|
update(csv_export_queued: false, xlsx_export_queued: false, ods_export_queued: false)
|
2019-10-03 15:35:31 +02:00
|
|
|
|
end
|
|
|
|
|
|
2018-05-17 15:41:44 +02:00
|
|
|
|
def locked?
|
|
|
|
|
publiee_ou_archivee?
|
|
|
|
|
end
|
|
|
|
|
|
2019-03-27 11:43:04 +01:00
|
|
|
|
def accepts_new_dossiers?
|
|
|
|
|
!archivee?
|
|
|
|
|
end
|
|
|
|
|
|
2018-05-17 15:41:44 +02:00
|
|
|
|
def publiee_ou_archivee?
|
|
|
|
|
publiee? || archivee?
|
|
|
|
|
end
|
|
|
|
|
|
2018-10-31 13:28:39 +01:00
|
|
|
|
def expose_legacy_carto_api?
|
2018-11-27 15:53:13 +01:00
|
|
|
|
module_api_carto&.use_api_carto? && module_api_carto&.migrated?
|
2018-10-31 13:28:39 +01:00
|
|
|
|
end
|
|
|
|
|
|
2019-05-23 14:28:14 +02:00
|
|
|
|
def declarative?
|
|
|
|
|
declarative_with_state.present?
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def declarative_accepte?
|
|
|
|
|
declarative_with_state == Procedure.declarative_with_states.fetch(:accepte)
|
|
|
|
|
end
|
|
|
|
|
|
2018-04-13 16:11:37 +02:00
|
|
|
|
# Warning: dossier after_save build_default_champs must be removed
|
|
|
|
|
# to save a dossier created from this method
|
|
|
|
|
def new_dossier
|
2019-08-26 15:31:44 +02:00
|
|
|
|
Dossier.new(
|
|
|
|
|
procedure: self,
|
|
|
|
|
champs: build_champs,
|
|
|
|
|
champs_private: build_champs_private,
|
|
|
|
|
groupe_instructeur: defaut_groupe_instructeur
|
|
|
|
|
)
|
2019-02-07 10:44:15 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def build_champs
|
|
|
|
|
types_de_champ.map(&:build_champ)
|
|
|
|
|
end
|
2018-04-13 16:11:37 +02:00
|
|
|
|
|
2019-02-07 10:44:15 +01:00
|
|
|
|
def build_champs_private
|
|
|
|
|
types_de_champ_private.map(&:build_champ)
|
2018-04-13 16:11:37 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-07-05 14:38:20 +02:00
|
|
|
|
def path_customized?
|
|
|
|
|
!path.match?(/[[:xdigit:]]{8}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{12}/)
|
|
|
|
|
end
|
|
|
|
|
|
2018-07-03 20:18:49 +02:00
|
|
|
|
def organisation_name
|
|
|
|
|
service&.nom || organisation
|
|
|
|
|
end
|
|
|
|
|
|
2018-03-20 17:47:37 +01:00
|
|
|
|
def self.active(id)
|
2017-07-11 15:52:06 +02:00
|
|
|
|
publiees.find(id)
|
2016-06-09 17:49:38 +02:00
|
|
|
|
end
|
|
|
|
|
|
2018-03-20 17:47:37 +01:00
|
|
|
|
def switch_types_de_champ(index_of_first_element)
|
2018-12-20 12:00:27 +01:00
|
|
|
|
switch_list_order(types_de_champ, index_of_first_element)
|
2016-06-08 16:45:18 +02:00
|
|
|
|
end
|
|
|
|
|
|
2018-03-20 17:47:37 +01:00
|
|
|
|
def switch_types_de_champ_private(index_of_first_element)
|
2018-12-20 12:00:27 +01:00
|
|
|
|
switch_list_order(types_de_champ_private, index_of_first_element)
|
2016-08-03 18:19:56 +02:00
|
|
|
|
end
|
|
|
|
|
|
2016-06-08 16:45:18 +02:00
|
|
|
|
def switch_list_order(list, index_of_first_element)
|
2017-05-26 21:43:44 +02:00
|
|
|
|
if index_of_first_element < 0 ||
|
|
|
|
|
index_of_first_element == list.count - 1 ||
|
|
|
|
|
list.count < 1
|
|
|
|
|
|
|
|
|
|
false
|
|
|
|
|
else
|
2018-03-02 16:27:03 +01:00
|
|
|
|
list[index_of_first_element].update(order_place: index_of_first_element + 1)
|
|
|
|
|
list[index_of_first_element + 1].update(order_place: index_of_first_element)
|
2019-01-03 15:12:55 +01:00
|
|
|
|
reload
|
2017-05-26 21:43:44 +02:00
|
|
|
|
|
|
|
|
|
true
|
|
|
|
|
end
|
2015-11-19 11:37:01 +01:00
|
|
|
|
end
|
2015-12-21 14:40:28 +01:00
|
|
|
|
|
2018-04-12 18:35:13 +02:00
|
|
|
|
def clone(admin, from_library)
|
2019-01-29 11:49:28 +01:00
|
|
|
|
is_different_admin = !admin.owns?(self)
|
2019-01-10 13:42:40 +01:00
|
|
|
|
|
2018-11-27 14:52:20 +01:00
|
|
|
|
populate_champ_stable_ids
|
2017-03-07 18:19:48 +01:00
|
|
|
|
procedure = self.deep_clone(include:
|
2017-06-26 17:18:47 +02:00
|
|
|
|
{
|
|
|
|
|
attestation_template: nil,
|
2019-02-06 10:02:05 +01:00
|
|
|
|
types_de_champ: [:drop_down_list, types_de_champ: :drop_down_list],
|
|
|
|
|
types_de_champ_private: [:drop_down_list, types_de_champ: :drop_down_list]
|
2019-03-26 11:00:24 +01:00
|
|
|
|
}, &method(:clone_attachments))
|
2019-07-04 16:15:40 +02:00
|
|
|
|
procedure.path = SecureRandom.uuid
|
2018-05-28 14:58:40 +02:00
|
|
|
|
procedure.aasm_state = :brouillon
|
|
|
|
|
procedure.test_started_at = nil
|
2017-07-10 23:42:33 +02:00
|
|
|
|
procedure.archived_at = nil
|
2017-07-11 14:21:10 +02:00
|
|
|
|
procedure.published_at = nil
|
2018-12-06 13:51:41 +01:00
|
|
|
|
procedure.lien_notice = nil
|
2017-03-07 18:19:48 +01:00
|
|
|
|
|
2019-01-10 13:43:22 +01:00
|
|
|
|
if is_different_admin || from_library
|
|
|
|
|
procedure.types_de_champ.each { |tdc| tdc.options&.delete(:old_pj) }
|
|
|
|
|
end
|
|
|
|
|
|
2019-01-29 11:49:28 +01:00
|
|
|
|
if is_different_admin
|
|
|
|
|
procedure.administrateurs = [admin]
|
|
|
|
|
else
|
|
|
|
|
procedure.administrateurs = administrateurs
|
|
|
|
|
end
|
|
|
|
|
|
2018-05-30 18:45:46 +02:00
|
|
|
|
procedure.initiated_mail = initiated_mail&.dup
|
|
|
|
|
procedure.received_mail = received_mail&.dup
|
|
|
|
|
procedure.closed_mail = closed_mail&.dup
|
|
|
|
|
procedure.refused_mail = refused_mail&.dup
|
|
|
|
|
procedure.without_continuation_mail = without_continuation_mail&.dup
|
2019-08-27 15:05:56 +02:00
|
|
|
|
procedure.ask_birthday = false # see issue #4242
|
2017-03-07 18:19:48 +01:00
|
|
|
|
|
2018-04-12 18:35:13 +02:00
|
|
|
|
procedure.cloned_from_library = from_library
|
2018-04-24 15:23:07 +02:00
|
|
|
|
procedure.parent_procedure = self
|
2018-04-12 18:35:13 +02:00
|
|
|
|
|
2018-06-28 11:33:10 +02:00
|
|
|
|
if from_library
|
|
|
|
|
procedure.service = nil
|
2019-01-10 13:42:40 +01:00
|
|
|
|
elsif self.service.present? && is_different_admin
|
2018-12-19 15:35:07 +01:00
|
|
|
|
procedure.service = self.service.clone_and_assign_to_administrateur(admin)
|
2018-06-28 11:33:10 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-08-21 16:33:26 +02:00
|
|
|
|
procedure.save
|
|
|
|
|
|
2019-08-06 11:02:54 +02:00
|
|
|
|
admin.instructeur.assign_to_procedure(procedure)
|
2019-01-04 14:45:06 +01:00
|
|
|
|
|
2018-01-08 15:42:38 +01:00
|
|
|
|
procedure
|
2016-06-15 11:34:05 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-03-28 17:17:29 +01:00
|
|
|
|
def clone_attachments(original, kopy)
|
|
|
|
|
if original.is_a?(TypeDeChamp)
|
|
|
|
|
clone_attachment(:piece_justificative_template, original, kopy)
|
|
|
|
|
elsif original.is_a?(Procedure)
|
2019-08-28 13:11:58 +02:00
|
|
|
|
clone_attachment(:logo, original, kopy)
|
2019-03-28 17:17:29 +01:00
|
|
|
|
clone_attachment(:notice, original, kopy)
|
|
|
|
|
clone_attachment(:deliberation, original, kopy)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def clone_attachment(attribute, original, kopy)
|
|
|
|
|
original_attachment = original.send(attribute)
|
|
|
|
|
if original_attachment.attached?
|
|
|
|
|
kopy.send(attribute).attach({
|
|
|
|
|
io: StringIO.new(original_attachment.download),
|
2019-08-20 15:54:28 +02:00
|
|
|
|
filename: original_attachment.filename,
|
|
|
|
|
content_type: original_attachment.content_type,
|
|
|
|
|
# we don't want to run virus scanner on cloned file
|
|
|
|
|
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
|
2019-03-28 17:17:29 +01:00
|
|
|
|
})
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-01-10 17:52:43 +01:00
|
|
|
|
def whitelisted?
|
|
|
|
|
whitelisted_at.present?
|
|
|
|
|
end
|
|
|
|
|
|
2016-07-22 15:06:30 +02:00
|
|
|
|
def total_dossier
|
2017-07-10 16:11:12 +02:00
|
|
|
|
self.dossiers.state_not_brouillon.size
|
2016-07-22 15:06:30 +02:00
|
|
|
|
end
|
2017-01-26 12:12:52 +01:00
|
|
|
|
|
2018-11-22 00:14:16 +01:00
|
|
|
|
def export_filename(format)
|
2018-09-13 18:33:35 +02:00
|
|
|
|
procedure_identifier = path || "procedure-#{id}"
|
2018-11-22 00:14:16 +01:00
|
|
|
|
"dossiers_#{procedure_identifier}_#{Time.zone.now.strftime('%Y-%m-%d_%H-%M')}.#{format}"
|
2018-03-16 12:00:01 +01:00
|
|
|
|
end
|
|
|
|
|
|
2019-09-18 16:51:45 +02:00
|
|
|
|
def export(dossiers, options = {})
|
2019-04-03 14:29:30 +02:00
|
|
|
|
version = options.delete(:version)
|
|
|
|
|
if version == 'v2'
|
|
|
|
|
options.delete(:tables)
|
2019-09-18 16:51:45 +02:00
|
|
|
|
ProcedureExportV2Service.new(self, dossiers, **options.to_h.symbolize_keys)
|
2019-04-03 14:29:30 +02:00
|
|
|
|
else
|
2019-09-18 16:51:45 +02:00
|
|
|
|
ProcedureExportService.new(self, dossiers, **options.to_h.symbolize_keys)
|
2019-04-03 14:29:30 +02:00
|
|
|
|
end
|
2018-11-22 00:14:16 +01:00
|
|
|
|
end
|
2017-04-05 11:34:16 +02:00
|
|
|
|
|
2019-09-18 16:51:45 +02:00
|
|
|
|
def to_csv(dossiers, options = {})
|
|
|
|
|
export(dossiers, options).to_csv
|
2018-11-22 00:14:16 +01:00
|
|
|
|
end
|
2017-04-05 11:34:16 +02:00
|
|
|
|
|
2019-09-18 16:51:45 +02:00
|
|
|
|
def to_xlsx(dossiers, options = {})
|
|
|
|
|
export(dossiers, options).to_xlsx
|
2018-11-22 00:14:16 +01:00
|
|
|
|
end
|
|
|
|
|
|
2019-09-18 16:51:45 +02:00
|
|
|
|
def to_ods(dossiers, options = {})
|
|
|
|
|
export(dossiers, options).to_ods
|
2017-04-05 11:34:16 +02:00
|
|
|
|
end
|
|
|
|
|
|
2017-06-27 18:00:05 +02:00
|
|
|
|
def procedure_overview(start_date)
|
|
|
|
|
ProcedureOverview.new(self, start_date)
|
2017-05-12 15:47:05 +02:00
|
|
|
|
end
|
2017-05-26 21:55:19 +02:00
|
|
|
|
|
2017-05-27 01:08:01 +02:00
|
|
|
|
def initiated_mail_template
|
2017-12-22 21:37:08 +01:00
|
|
|
|
initiated_mail || Mails::InitiatedMail.default_for_procedure(self)
|
2017-05-26 21:55:19 +02:00
|
|
|
|
end
|
|
|
|
|
|
2017-05-27 01:08:01 +02:00
|
|
|
|
def received_mail_template
|
2017-12-22 21:37:08 +01:00
|
|
|
|
received_mail || Mails::ReceivedMail.default_for_procedure(self)
|
2017-05-26 21:55:19 +02:00
|
|
|
|
end
|
|
|
|
|
|
2017-05-27 01:08:01 +02:00
|
|
|
|
def closed_mail_template
|
2017-12-22 21:37:08 +01:00
|
|
|
|
closed_mail || Mails::ClosedMail.default_for_procedure(self)
|
2017-05-26 21:55:19 +02:00
|
|
|
|
end
|
|
|
|
|
|
2017-05-27 01:08:01 +02:00
|
|
|
|
def refused_mail_template
|
2017-12-22 21:37:08 +01:00
|
|
|
|
refused_mail || Mails::RefusedMail.default_for_procedure(self)
|
2017-05-26 21:55:19 +02:00
|
|
|
|
end
|
|
|
|
|
|
2017-05-27 01:08:01 +02:00
|
|
|
|
def without_continuation_mail_template
|
2017-12-22 21:37:08 +01:00
|
|
|
|
without_continuation_mail || Mails::WithoutContinuationMail.default_for_procedure(self)
|
2017-05-26 21:55:19 +02:00
|
|
|
|
end
|
2017-10-02 17:03:38 +02:00
|
|
|
|
|
2017-09-27 15:16:07 +02:00
|
|
|
|
def self.default_sort
|
|
|
|
|
{
|
|
|
|
|
'table' => 'self',
|
|
|
|
|
'column' => 'id',
|
|
|
|
|
'order' => 'desc'
|
2018-09-20 17:02:28 +02:00
|
|
|
|
}
|
2017-09-27 15:16:07 +02:00
|
|
|
|
end
|
|
|
|
|
|
2018-01-10 16:46:12 +01:00
|
|
|
|
def whitelist!
|
2018-10-25 15:07:15 +02:00
|
|
|
|
update_attribute('whitelisted_at', Time.zone.now)
|
2018-01-10 16:46:12 +01:00
|
|
|
|
end
|
|
|
|
|
|
2018-04-03 11:33:11 +02:00
|
|
|
|
def closed_mail_template_attestation_inconsistency_state
|
|
|
|
|
# As an optimization, don’t check the predefined templates (they are presumed correct)
|
|
|
|
|
if closed_mail.present?
|
|
|
|
|
tag_present = closed_mail.body.include?("--lien attestation--")
|
|
|
|
|
if attestation_template&.activated? && !tag_present
|
|
|
|
|
:missing_tag
|
|
|
|
|
elsif !attestation_template&.activated? && tag_present
|
|
|
|
|
:extraneous_tag
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-11-08 17:19:17 +01:00
|
|
|
|
def usual_traitement_time
|
2018-11-08 18:09:27 +01:00
|
|
|
|
percentile_time(:en_construction_at, :processed_at, 90)
|
2018-09-18 17:50:05 +02:00
|
|
|
|
end
|
|
|
|
|
|
2018-11-27 14:52:20 +01:00
|
|
|
|
def populate_champ_stable_ids
|
|
|
|
|
TypeDeChamp.where(procedure: self, stable_id: nil).find_each do |type_de_champ|
|
|
|
|
|
type_de_champ.update_column(:stable_id, type_de_champ.id)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-01-04 15:43:02 +01:00
|
|
|
|
def missing_steps
|
|
|
|
|
result = []
|
|
|
|
|
|
|
|
|
|
if service.nil?
|
|
|
|
|
result << :service
|
|
|
|
|
end
|
|
|
|
|
|
2019-08-21 13:53:53 +02:00
|
|
|
|
if missing_instructeurs?
|
2019-01-04 15:43:02 +01:00
|
|
|
|
result << :instructeurs
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
result
|
|
|
|
|
end
|
|
|
|
|
|
2019-03-20 14:27:30 +01:00
|
|
|
|
def move_type_de_champ(type_de_champ, new_index)
|
|
|
|
|
types_de_champ, collection_attribute_name = if type_de_champ.parent&.repetition?
|
|
|
|
|
if type_de_champ.parent.private?
|
|
|
|
|
[type_de_champ.parent.types_de_champ, :types_de_champ_private_attributes]
|
|
|
|
|
else
|
|
|
|
|
[type_de_champ.parent.types_de_champ, :types_de_champ_attributes]
|
|
|
|
|
end
|
|
|
|
|
elsif type_de_champ.private?
|
|
|
|
|
[self.types_de_champ_private, :types_de_champ_private_attributes]
|
|
|
|
|
else
|
|
|
|
|
[self.types_de_champ, :types_de_champ_attributes]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
attributes = move_type_de_champ_attributes(types_de_champ.to_a, type_de_champ, new_index)
|
|
|
|
|
|
|
|
|
|
if type_de_champ.parent&.repetition?
|
|
|
|
|
attributes = [
|
|
|
|
|
{
|
|
|
|
|
id: type_de_champ.parent.id,
|
|
|
|
|
libelle: type_de_champ.parent.libelle,
|
|
|
|
|
types_de_champ_attributes: attributes
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
update!(collection_attribute_name => attributes)
|
|
|
|
|
end
|
|
|
|
|
|
2019-05-23 14:28:14 +02:00
|
|
|
|
def process_dossiers!
|
|
|
|
|
case declarative_with_state
|
|
|
|
|
when Procedure.declarative_with_states.fetch(:en_instruction)
|
|
|
|
|
dossiers
|
|
|
|
|
.state_en_construction
|
|
|
|
|
.find_each(&:passer_automatiquement_en_instruction!)
|
|
|
|
|
when Procedure.declarative_with_states.fetch(:accepte)
|
|
|
|
|
dossiers
|
|
|
|
|
.state_en_construction
|
|
|
|
|
.find_each(&:accepter_automatiquement!)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-08-20 11:28:07 +02:00
|
|
|
|
def logo_url
|
2019-08-28 13:11:58 +02:00
|
|
|
|
if logo.attached?
|
|
|
|
|
Rails.application.routes.url_helpers.url_for(logo)
|
2019-08-20 11:28:07 +02:00
|
|
|
|
else
|
2019-08-28 13:11:58 +02:00
|
|
|
|
ActionController::Base.helpers.image_url("marianne.svg")
|
2019-08-20 11:28:07 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-08-21 13:53:53 +02:00
|
|
|
|
def missing_instructeurs?
|
|
|
|
|
!AssignTo.exists?(groupe_instructeur: groupe_instructeurs)
|
|
|
|
|
end
|
|
|
|
|
|
2019-09-18 21:58:23 +02:00
|
|
|
|
def routee?
|
|
|
|
|
groupe_instructeurs.count > 1
|
|
|
|
|
end
|
|
|
|
|
|
2017-10-02 17:03:38 +02:00
|
|
|
|
private
|
|
|
|
|
|
2019-03-20 14:27:30 +01:00
|
|
|
|
def move_type_de_champ_attributes(types_de_champ, type_de_champ, new_index)
|
|
|
|
|
old_index = types_de_champ.index(type_de_champ)
|
|
|
|
|
types_de_champ.insert(new_index, types_de_champ.delete_at(old_index))
|
|
|
|
|
.map.with_index do |type_de_champ, index|
|
|
|
|
|
{
|
|
|
|
|
id: type_de_champ.id,
|
|
|
|
|
libelle: type_de_champ.libelle,
|
|
|
|
|
order_place: index
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-07-30 16:54:43 +02:00
|
|
|
|
def before_publish
|
|
|
|
|
update!(archived_at: nil)
|
2018-09-07 18:36:31 +02:00
|
|
|
|
end
|
|
|
|
|
|
2019-07-30 16:54:43 +02:00
|
|
|
|
def after_publish
|
2018-10-25 15:11:12 +02:00
|
|
|
|
update!(published_at: Time.zone.now)
|
2018-09-07 18:36:31 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def after_archive
|
2019-09-16 17:00:37 +02:00
|
|
|
|
update!(archived_at: Time.zone.now)
|
2019-10-03 15:35:31 +02:00
|
|
|
|
purge_export_files
|
2018-09-07 18:36:31 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def after_hide
|
2018-10-25 15:11:12 +02:00
|
|
|
|
now = Time.zone.now
|
2019-09-16 17:00:37 +02:00
|
|
|
|
update!(hidden_at: now)
|
2018-09-07 18:36:31 +02:00
|
|
|
|
dossiers.update_all(hidden_at: now)
|
2019-10-03 15:35:31 +02:00
|
|
|
|
purge_export_files
|
2018-09-07 18:36:31 +02:00
|
|
|
|
end
|
|
|
|
|
|
2018-10-09 12:11:13 +02:00
|
|
|
|
def after_draft
|
|
|
|
|
update!(published_at: nil)
|
|
|
|
|
end
|
|
|
|
|
|
2018-06-01 11:06:12 +02:00
|
|
|
|
def update_juridique_required
|
|
|
|
|
self.juridique_required ||= (cadre_juridique.present? || deliberation.attached?)
|
|
|
|
|
true
|
|
|
|
|
end
|
|
|
|
|
|
2018-05-31 10:59:38 +02:00
|
|
|
|
def check_juridique
|
2018-06-01 10:51:04 +02:00
|
|
|
|
if juridique_required? && (cadre_juridique.blank? && !deliberation.attached?)
|
2018-05-31 10:59:38 +02:00
|
|
|
|
errors.add(:cadre_juridique, " : veuillez remplir le texte de loi ou la délibération")
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2018-05-25 00:16:13 +02:00
|
|
|
|
def update_durees_conservation_required
|
|
|
|
|
self.durees_conservation_required ||= duree_conservation_dossiers_hors_ds.present? && duree_conservation_dossiers_dans_ds.present?
|
|
|
|
|
true
|
|
|
|
|
end
|
2018-09-19 11:02:38 +02:00
|
|
|
|
|
2018-11-08 18:09:27 +01:00
|
|
|
|
def percentile_time(start_attribute, end_attribute, p)
|
2018-09-19 11:02:38 +02:00
|
|
|
|
times = dossiers
|
2019-03-20 17:45:41 +01:00
|
|
|
|
.where.not(start_attribute => nil, end_attribute => nil)
|
2019-02-18 11:56:09 +01:00
|
|
|
|
.where(end_attribute => 1.month.ago..Time.zone.now)
|
2018-09-19 11:02:38 +02:00
|
|
|
|
.pluck(start_attribute, end_attribute)
|
2018-09-27 14:30:31 +02:00
|
|
|
|
.map { |(start_date, end_date)| end_date - start_date }
|
2018-09-19 11:02:38 +02:00
|
|
|
|
|
|
|
|
|
if times.present?
|
2018-11-08 18:09:27 +01:00
|
|
|
|
times.percentile(p).ceil
|
2018-09-19 11:02:38 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
2019-01-09 16:35:06 +01:00
|
|
|
|
|
|
|
|
|
def ensure_path_exists
|
2019-09-16 17:00:37 +02:00
|
|
|
|
if self.path.blank?
|
2019-03-06 14:42:27 +01:00
|
|
|
|
self.path = SecureRandom.uuid
|
2019-01-09 16:35:06 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
2019-08-27 15:28:02 +02:00
|
|
|
|
|
|
|
|
|
def ensure_default_groupe_instructeur
|
|
|
|
|
if self.groupe_instructeurs.empty?
|
|
|
|
|
groupe_instructeurs.create(label: GroupeInstructeur::DEFAULT_LABEL)
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-09-21 17:59:03 +02:00
|
|
|
|
end
|