2020-08-06 16:35:45 +02:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: procedure_revisions
|
|
|
|
#
|
2022-01-25 18:26:34 +01:00
|
|
|
# id :bigint not null, primary key
|
|
|
|
# published_at :datetime
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# procedure_id :bigint not null
|
2020-08-06 16:35:45 +02:00
|
|
|
#
|
2020-06-26 11:37:28 +02:00
|
|
|
class ProcedureRevision < ApplicationRecord
|
2020-07-22 11:56:19 +02:00
|
|
|
self.implicit_order_column = :created_at
|
2020-08-27 19:55:10 +02:00
|
|
|
belongs_to :procedure, -> { with_discarded }, inverse_of: :revisions, optional: false
|
2020-06-26 11:37:28 +02:00
|
|
|
|
2020-09-17 11:15:21 +02:00
|
|
|
has_many :dossiers, inverse_of: :revision, foreign_key: :revision_id
|
|
|
|
|
2021-12-06 21:04:01 +01:00
|
|
|
has_many :revision_types_de_champ, -> { root.public_only.ordered }, class_name: 'ProcedureRevisionTypeDeChamp', foreign_key: :revision_id, dependent: :destroy, inverse_of: :revision
|
|
|
|
has_many :revision_types_de_champ_private, -> { root.private_only.ordered }, class_name: 'ProcedureRevisionTypeDeChamp', foreign_key: :revision_id, dependent: :destroy, inverse_of: :revision
|
2020-06-26 11:37:28 +02:00
|
|
|
has_many :types_de_champ, through: :revision_types_de_champ, source: :type_de_champ
|
|
|
|
has_many :types_de_champ_private, through: :revision_types_de_champ_private, source: :type_de_champ
|
|
|
|
|
2021-04-13 20:24:12 +02:00
|
|
|
has_many :owned_types_de_champ, class_name: 'TypeDeChamp', foreign_key: :revision_id, dependent: :destroy, inverse_of: :revision
|
2021-05-27 12:17:10 +02:00
|
|
|
has_one :draft_procedure, -> { with_discarded }, class_name: 'Procedure', foreign_key: :draft_revision_id, dependent: :nullify, inverse_of: :draft_revision
|
|
|
|
has_one :published_procedure, -> { with_discarded }, class_name: 'Procedure', foreign_key: :published_revision_id, dependent: :nullify, inverse_of: :published_revision
|
2021-04-13 20:24:12 +02:00
|
|
|
|
2021-12-06 21:04:01 +01:00
|
|
|
scope :ordered, -> { order(:created_at) }
|
|
|
|
|
2020-08-27 19:55:10 +02:00
|
|
|
def build_champs
|
|
|
|
types_de_champ.map(&:build_champ)
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_champs_private
|
|
|
|
types_de_champ_private.map(&:build_champ)
|
|
|
|
end
|
|
|
|
|
2020-06-26 11:42:24 +02:00
|
|
|
def add_type_de_champ(params)
|
|
|
|
params[:revision] = self
|
|
|
|
|
|
|
|
if params[:parent_id]
|
|
|
|
find_or_clone_type_de_champ(params.delete(:parent_id))
|
|
|
|
.types_de_champ
|
|
|
|
.tap do |types_de_champ|
|
2020-07-22 11:09:51 +02:00
|
|
|
params[:order_place] = types_de_champ.present? ? types_de_champ.last.order_place + 1 : 0
|
2021-12-06 21:04:01 +01:00
|
|
|
end.create(params).migrate_parent!
|
2020-06-26 11:42:24 +02:00
|
|
|
elsif params[:private]
|
2020-08-27 19:55:10 +02:00
|
|
|
types_de_champ_private.create(params)
|
2020-06-26 11:42:24 +02:00
|
|
|
else
|
2020-08-27 19:55:10 +02:00
|
|
|
types_de_champ.create(params)
|
2020-06-26 11:42:24 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_or_clone_type_de_champ(id)
|
|
|
|
type_de_champ = find_type_de_champ_by_id(id)
|
|
|
|
|
|
|
|
if type_de_champ.revision == self
|
|
|
|
type_de_champ
|
|
|
|
elsif type_de_champ.parent.present?
|
|
|
|
find_or_clone_type_de_champ(type_de_champ.parent.stable_id).types_de_champ.find_by!(stable_id: id)
|
|
|
|
else
|
2020-09-08 15:53:07 +02:00
|
|
|
revise_type_de_champ(type_de_champ)
|
2020-06-26 11:42:24 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def move_type_de_champ(id, position)
|
|
|
|
type_de_champ = find_type_de_champ_by_id(id)
|
|
|
|
|
|
|
|
if type_de_champ.parent.present?
|
|
|
|
repetition_type_de_champ = find_or_clone_type_de_champ(id).parent
|
|
|
|
|
|
|
|
move_type_de_champ_hash(repetition_type_de_champ.types_de_champ.to_a, type_de_champ, position).each do |(id, position)|
|
2021-12-06 21:04:01 +01:00
|
|
|
type_de_champ = repetition_type_de_champ.types_de_champ.find(id)
|
|
|
|
type_de_champ.update!(order_place: position)
|
|
|
|
type_de_champ.revision_type_de_champ&.update!(position: position)
|
2020-06-26 11:42:24 +02:00
|
|
|
end
|
|
|
|
elsif type_de_champ.private?
|
|
|
|
move_type_de_champ_hash(types_de_champ_private.to_a, type_de_champ, position).each do |(id, position)|
|
|
|
|
revision_types_de_champ_private.find_by!(type_de_champ_id: id).update!(position: position)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
move_type_de_champ_hash(types_de_champ.to_a, type_de_champ, position).each do |(id, position)|
|
|
|
|
revision_types_de_champ.find_by!(type_de_champ_id: id).update!(position: position)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_type_de_champ(id)
|
|
|
|
type_de_champ = find_type_de_champ_by_id(id)
|
|
|
|
|
|
|
|
if type_de_champ.revision == self
|
|
|
|
type_de_champ.destroy
|
|
|
|
elsif type_de_champ.parent.present?
|
|
|
|
find_or_clone_type_de_champ(id).destroy
|
|
|
|
elsif type_de_champ.private?
|
|
|
|
types_de_champ_private.delete(type_de_champ)
|
|
|
|
else
|
|
|
|
types_de_champ.delete(type_de_champ)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-26 11:37:28 +02:00
|
|
|
def draft?
|
|
|
|
procedure.draft_revision == self
|
|
|
|
end
|
|
|
|
|
|
|
|
def locked?
|
|
|
|
!draft?
|
|
|
|
end
|
2020-06-26 11:42:24 +02:00
|
|
|
|
2022-01-12 17:38:57 +01:00
|
|
|
def different_from?(revision)
|
2021-01-20 13:21:23 +01:00
|
|
|
types_de_champ != revision.types_de_champ || types_de_champ_private != revision.types_de_champ_private
|
|
|
|
end
|
|
|
|
|
|
|
|
def compare(revision)
|
|
|
|
changes = []
|
|
|
|
changes += compare_types_de_champ(types_de_champ, revision.types_de_champ)
|
|
|
|
changes += compare_types_de_champ(types_de_champ_private, revision.types_de_champ_private)
|
|
|
|
changes
|
|
|
|
end
|
|
|
|
|
2021-06-23 15:57:11 +02:00
|
|
|
def new_dossier
|
|
|
|
Dossier.new(
|
|
|
|
revision: self,
|
|
|
|
champs: build_champs,
|
|
|
|
champs_private: build_champs_private,
|
|
|
|
groupe_instructeur: procedure.defaut_groupe_instructeur
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-06-26 11:42:24 +02:00
|
|
|
private
|
|
|
|
|
2021-01-20 13:21:23 +01:00
|
|
|
def compare_types_de_champ(from_tdc, to_tdc)
|
|
|
|
if from_tdc == to_tdc
|
|
|
|
[]
|
|
|
|
else
|
|
|
|
from_h = from_tdc.index_by(&:stable_id)
|
|
|
|
to_h = to_tdc.index_by(&:stable_id)
|
|
|
|
|
|
|
|
from_sids = from_h.keys
|
|
|
|
to_sids = to_h.keys
|
|
|
|
|
|
|
|
removed = (from_sids - to_sids).map do |sid|
|
2021-09-20 16:24:40 +02:00
|
|
|
{ op: :remove, label: from_h[sid].libelle, private: from_h[sid].private?, position: from_sids.index(sid), stable_id: sid }
|
2021-01-20 13:21:23 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
added = (to_sids - from_sids).map do |sid|
|
2021-09-20 16:24:40 +02:00
|
|
|
{ op: :add, label: to_h[sid].libelle, private: to_h[sid].private?, position: to_sids.index(sid), stable_id: sid }
|
2021-01-20 13:21:23 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
kept = from_sids.intersection(to_sids)
|
|
|
|
|
|
|
|
moved = kept
|
|
|
|
.map { |sid| [sid, from_sids.index(sid), to_sids.index(sid)] }
|
|
|
|
.filter { |_, from_index, to_index| from_index != to_index }
|
|
|
|
.map do |sid, from_index, to_index|
|
2021-09-20 16:24:40 +02:00
|
|
|
{ op: :move, label: from_h[sid].libelle, private: from_h[sid].private?, from: from_index, to: to_index, position: to_index, stable_id: sid }
|
2021-01-20 13:21:23 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
changed = kept
|
|
|
|
.map { |sid| [sid, from_h[sid], to_h[sid]] }
|
|
|
|
.flat_map do |sid, from, to|
|
|
|
|
compare_type_de_champ(from, to)
|
|
|
|
.each { |h| h[:position] = to_sids.index(sid) }
|
|
|
|
end
|
|
|
|
|
|
|
|
(removed + added + moved + changed)
|
|
|
|
.sort_by { |h| h[:position] }
|
|
|
|
.each { |h| h.delete(:position) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def compare_type_de_champ(from_type_de_champ, to_type_de_champ)
|
|
|
|
changes = []
|
|
|
|
if from_type_de_champ.type_champ != to_type_de_champ.type_champ
|
|
|
|
changes << {
|
|
|
|
op: :update,
|
|
|
|
attribute: :type_champ,
|
|
|
|
label: from_type_de_champ.libelle,
|
2021-06-22 12:20:04 +02:00
|
|
|
private: from_type_de_champ.private?,
|
2021-01-20 13:21:23 +01:00
|
|
|
from: from_type_de_champ.type_champ,
|
2021-09-20 16:24:40 +02:00
|
|
|
to: to_type_de_champ.type_champ,
|
|
|
|
stable_id: from_type_de_champ.stable_id
|
2021-01-20 13:21:23 +01:00
|
|
|
}
|
|
|
|
end
|
|
|
|
if from_type_de_champ.libelle != to_type_de_champ.libelle
|
|
|
|
changes << {
|
|
|
|
op: :update,
|
|
|
|
attribute: :libelle,
|
|
|
|
label: from_type_de_champ.libelle,
|
2021-06-22 12:20:04 +02:00
|
|
|
private: from_type_de_champ.private?,
|
2021-01-20 13:21:23 +01:00
|
|
|
from: from_type_de_champ.libelle,
|
2021-09-20 16:24:40 +02:00
|
|
|
to: to_type_de_champ.libelle,
|
|
|
|
stable_id: from_type_de_champ.stable_id
|
2021-01-20 13:21:23 +01:00
|
|
|
}
|
|
|
|
end
|
|
|
|
if from_type_de_champ.description != to_type_de_champ.description
|
|
|
|
changes << {
|
|
|
|
op: :update,
|
|
|
|
attribute: :description,
|
|
|
|
label: from_type_de_champ.libelle,
|
2021-06-22 12:20:04 +02:00
|
|
|
private: from_type_de_champ.private?,
|
2021-01-20 13:21:23 +01:00
|
|
|
from: from_type_de_champ.description,
|
2021-09-20 16:24:40 +02:00
|
|
|
to: to_type_de_champ.description,
|
|
|
|
stable_id: from_type_de_champ.stable_id
|
2021-01-20 13:21:23 +01:00
|
|
|
}
|
|
|
|
end
|
|
|
|
if from_type_de_champ.mandatory? != to_type_de_champ.mandatory?
|
|
|
|
changes << {
|
|
|
|
op: :update,
|
|
|
|
attribute: :mandatory,
|
|
|
|
label: from_type_de_champ.libelle,
|
2021-06-22 12:20:04 +02:00
|
|
|
private: from_type_de_champ.private?,
|
2021-01-20 13:21:23 +01:00
|
|
|
from: from_type_de_champ.mandatory?,
|
2021-09-20 16:24:40 +02:00
|
|
|
to: to_type_de_champ.mandatory?,
|
|
|
|
stable_id: from_type_de_champ.stable_id
|
2021-01-20 13:21:23 +01:00
|
|
|
}
|
|
|
|
end
|
|
|
|
if to_type_de_champ.drop_down_list?
|
|
|
|
if from_type_de_champ.drop_down_list_options != to_type_de_champ.drop_down_list_options
|
|
|
|
changes << {
|
|
|
|
op: :update,
|
|
|
|
attribute: :drop_down_options,
|
|
|
|
label: from_type_de_champ.libelle,
|
2021-06-22 12:20:04 +02:00
|
|
|
private: from_type_de_champ.private?,
|
2021-01-20 13:21:23 +01:00
|
|
|
from: from_type_de_champ.drop_down_list_options,
|
2021-09-20 16:24:40 +02:00
|
|
|
to: to_type_de_champ.drop_down_list_options,
|
|
|
|
stable_id: from_type_de_champ.stable_id
|
2021-06-23 15:54:12 +02:00
|
|
|
}
|
|
|
|
end
|
2021-10-19 18:48:45 +02:00
|
|
|
if to_type_de_champ.linked_drop_down_list?
|
|
|
|
if from_type_de_champ.drop_down_secondary_libelle != to_type_de_champ.drop_down_secondary_libelle
|
|
|
|
changes << {
|
|
|
|
op: :update,
|
|
|
|
attribute: :drop_down_secondary_libelle,
|
|
|
|
label: from_type_de_champ.libelle,
|
|
|
|
private: from_type_de_champ.private?,
|
|
|
|
from: from_type_de_champ.drop_down_secondary_libelle,
|
|
|
|
to: to_type_de_champ.drop_down_secondary_libelle
|
|
|
|
}
|
|
|
|
end
|
|
|
|
if from_type_de_champ.drop_down_secondary_description != to_type_de_champ.drop_down_secondary_description
|
|
|
|
changes << {
|
|
|
|
op: :update,
|
|
|
|
attribute: :drop_down_secondary_description,
|
|
|
|
label: from_type_de_champ.libelle,
|
|
|
|
private: from_type_de_champ.private?,
|
|
|
|
from: from_type_de_champ.drop_down_secondary_description,
|
|
|
|
to: to_type_de_champ.drop_down_secondary_description
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2021-10-22 20:44:46 +02:00
|
|
|
if from_type_de_champ.drop_down_other != to_type_de_champ.drop_down_other
|
|
|
|
changes << {
|
|
|
|
op: :update,
|
|
|
|
attribute: :drop_down_other,
|
|
|
|
label: from_type_de_champ.libelle,
|
|
|
|
private: from_type_de_champ.private?,
|
|
|
|
from: from_type_de_champ.drop_down_other,
|
|
|
|
to: to_type_de_champ.drop_down_other,
|
|
|
|
stable_id: from_type_de_champ.stable_id
|
|
|
|
}
|
|
|
|
end
|
2021-06-23 15:54:12 +02:00
|
|
|
elsif to_type_de_champ.carte?
|
|
|
|
if from_type_de_champ.carte_optional_layers != to_type_de_champ.carte_optional_layers
|
|
|
|
changes << {
|
|
|
|
op: :update,
|
|
|
|
attribute: :carte_layers,
|
|
|
|
label: from_type_de_champ.libelle,
|
|
|
|
private: from_type_de_champ.private?,
|
|
|
|
from: from_type_de_champ.carte_optional_layers,
|
2021-09-20 16:24:40 +02:00
|
|
|
to: to_type_de_champ.carte_optional_layers,
|
|
|
|
stable_id: from_type_de_champ.stable_id
|
2021-01-20 13:21:23 +01:00
|
|
|
}
|
|
|
|
end
|
|
|
|
elsif to_type_de_champ.piece_justificative?
|
|
|
|
if from_type_de_champ.piece_justificative_template_checksum != to_type_de_champ.piece_justificative_template_checksum
|
|
|
|
changes << {
|
|
|
|
op: :update,
|
|
|
|
attribute: :piece_justificative_template,
|
|
|
|
label: from_type_de_champ.libelle,
|
2021-06-22 12:20:04 +02:00
|
|
|
private: from_type_de_champ.private?,
|
2021-01-20 13:21:23 +01:00
|
|
|
from: from_type_de_champ.piece_justificative_template_filename,
|
2021-09-20 16:24:40 +02:00
|
|
|
to: to_type_de_champ.piece_justificative_template_filename,
|
|
|
|
stable_id: from_type_de_champ.stable_id
|
2021-01-20 13:21:23 +01:00
|
|
|
}
|
|
|
|
end
|
|
|
|
elsif to_type_de_champ.repetition?
|
|
|
|
if from_type_de_champ.types_de_champ != to_type_de_champ.types_de_champ
|
|
|
|
changes += compare_types_de_champ(from_type_de_champ.types_de_champ, to_type_de_champ.types_de_champ)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
changes
|
|
|
|
end
|
|
|
|
|
2020-09-08 15:53:07 +02:00
|
|
|
def revise_type_de_champ(type_de_champ)
|
|
|
|
types_de_champ_association = type_de_champ.private? ? :revision_types_de_champ_private : :revision_types_de_champ
|
|
|
|
association = send(types_de_champ_association).find_by!(type_de_champ: type_de_champ)
|
2021-07-01 13:33:22 +02:00
|
|
|
cloned_type_de_champ = type_de_champ.deep_clone(include: [:types_de_champ]) do |original, kopy|
|
|
|
|
PiecesJustificativesService.clone_attachments(original, kopy)
|
|
|
|
end
|
2020-09-08 15:53:07 +02:00
|
|
|
cloned_type_de_champ.revision = self
|
|
|
|
association.update!(type_de_champ: cloned_type_de_champ)
|
2021-12-06 21:04:01 +01:00
|
|
|
cloned_type_de_champ.types_de_champ.each(&:migrate_parent!)
|
2020-09-08 15:53:07 +02:00
|
|
|
cloned_type_de_champ
|
|
|
|
end
|
|
|
|
|
2020-06-26 11:42:24 +02:00
|
|
|
def find_type_de_champ_by_id(id)
|
|
|
|
types_de_champ.find_by(stable_id: id) ||
|
|
|
|
types_de_champ_private.find_by(stable_id: id) ||
|
|
|
|
types_de_champ_in_repetition.find_by!(stable_id: id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def types_de_champ_in_repetition
|
|
|
|
parent_ids = types_de_champ.repetition.ids + types_de_champ_private.repetition.ids
|
|
|
|
TypeDeChamp.where(parent_id: parent_ids)
|
|
|
|
end
|
|
|
|
|
|
|
|
def move_type_de_champ_hash(types_de_champ, type_de_champ, new_index)
|
|
|
|
old_index = types_de_champ.index(type_de_champ)
|
|
|
|
|
|
|
|
if types_de_champ.delete_at(old_index)
|
|
|
|
types_de_champ.insert(new_index, type_de_champ)
|
|
|
|
.map.with_index do |type_de_champ, index|
|
|
|
|
[type_de_champ.id, index]
|
|
|
|
end
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
2020-06-26 11:37:28 +02:00
|
|
|
end
|