2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-03-02 22:13:09 +01:00
|
|
|
class ExportTemplate < ApplicationRecord
|
|
|
|
include TagsSubstitutionConcern
|
|
|
|
|
2024-07-23 17:05:16 +02:00
|
|
|
self.ignored_columns += ["content"]
|
|
|
|
|
2024-03-02 22:13:09 +01:00
|
|
|
belongs_to :groupe_instructeur
|
|
|
|
has_one :procedure, through: :groupe_instructeur
|
2024-03-12 17:28:43 +01:00
|
|
|
has_many :exports, dependent: :nullify
|
2024-03-03 10:19:02 +01:00
|
|
|
|
2024-10-25 14:36:38 +02:00
|
|
|
enum kind: { zip: 'zip', csv: 'csv', xlsx: 'xlsx', ods: 'ods' }, _prefix: :template
|
2024-03-02 22:13:09 +01:00
|
|
|
|
2024-07-23 17:05:16 +02:00
|
|
|
attribute :dossier_folder, :export_item
|
|
|
|
attribute :export_pdf, :export_item
|
|
|
|
attribute :pjs, :export_item, array: true
|
2024-03-02 22:13:09 +01:00
|
|
|
|
2024-10-25 14:21:45 +02:00
|
|
|
attribute :exported_columns, :exported_column, array: true
|
|
|
|
|
2024-07-23 17:05:16 +02:00
|
|
|
before_validation :ensure_pjs_are_legit
|
2024-03-02 22:13:09 +01:00
|
|
|
|
2024-07-23 17:05:16 +02:00
|
|
|
validates_with ExportTemplateValidator
|
2024-03-03 10:03:12 +01:00
|
|
|
|
2024-07-23 17:05:16 +02:00
|
|
|
DOSSIER_STATE = Dossier.states.fetch(:en_construction)
|
2024-03-03 10:03:12 +01:00
|
|
|
|
2024-07-23 17:05:16 +02:00
|
|
|
# when a pj has been added to a revision, it will not be present in the previous pjs
|
|
|
|
# a default value is provided.
|
|
|
|
def pj(tdc)
|
|
|
|
pjs.find { _1.stable_id == tdc.stable_id } || ExportItem.default_pj(tdc)
|
2024-03-02 22:13:09 +01:00
|
|
|
end
|
|
|
|
|
2024-07-23 17:05:16 +02:00
|
|
|
def self.default(name: nil, kind: 'zip', groupe_instructeur:)
|
2024-10-25 14:36:38 +02:00
|
|
|
# TODO: remove default values for tabular export
|
2024-07-23 17:05:16 +02:00
|
|
|
dossier_folder = ExportItem.default(prefix: 'dossier')
|
|
|
|
export_pdf = ExportItem.default(prefix: 'export')
|
|
|
|
pjs = groupe_instructeur.procedure.exportables_pieces_jointes.map { |tdc| ExportItem.default_pj(tdc) }
|
2024-03-02 22:13:09 +01:00
|
|
|
|
2024-07-23 17:05:16 +02:00
|
|
|
new(name:, kind:, groupe_instructeur:, dossier_folder:, export_pdf:, pjs:)
|
2024-03-02 22:13:09 +01:00
|
|
|
end
|
|
|
|
|
2024-10-25 14:36:38 +02:00
|
|
|
def tabular?
|
|
|
|
kind != 'zip'
|
|
|
|
end
|
|
|
|
|
2024-07-23 17:05:16 +02:00
|
|
|
def tags
|
2024-03-03 11:13:07 +01:00
|
|
|
tags_categorized.slice(:individual, :etablissement, :dossier).values.flatten
|
|
|
|
end
|
|
|
|
|
2024-07-23 17:05:16 +02:00
|
|
|
def pj_tags
|
|
|
|
tags.push(
|
2024-03-19 11:03:29 +01:00
|
|
|
libelle: 'nom original du fichier',
|
2024-07-23 17:05:16 +02:00
|
|
|
id: 'original-filename'
|
|
|
|
)
|
2024-03-20 16:34:46 +01:00
|
|
|
end
|
|
|
|
|
2024-07-23 17:05:16 +02:00
|
|
|
def attachment_path(dossier, attachment, index: 0, row_index: nil, champ: nil)
|
|
|
|
file_path = if attachment.name == 'pdf_export_for_instructeur'
|
|
|
|
export_pdf.path(dossier, attachment:)
|
|
|
|
elsif attachment.record_type == 'Champ' && pj(champ.type_de_champ).enabled?
|
|
|
|
pj(champ.type_de_champ).path(dossier, attachment:, index:, row_index:)
|
2024-03-02 22:13:09 +01:00
|
|
|
else
|
2024-07-23 17:05:16 +02:00
|
|
|
nil
|
2024-03-02 22:13:09 +01:00
|
|
|
end
|
|
|
|
|
2024-07-23 17:05:16 +02:00
|
|
|
File.join(dossier_folder.path(dossier), file_path) if file_path.present?
|
2024-03-02 22:13:09 +01:00
|
|
|
end
|
|
|
|
|
2024-10-25 14:36:38 +02:00
|
|
|
def dossier_exported_columns = exported_columns.filter { _1.column.dossier_column? }
|
|
|
|
|
|
|
|
def columns_for_stable_id(stable_id)
|
|
|
|
exported_columns
|
|
|
|
.filter { _1.column.champ_column? }
|
|
|
|
.filter { _1.column.stable_id == stable_id }
|
|
|
|
end
|
|
|
|
|
|
|
|
def in_export?(exported_column)
|
|
|
|
@template_exported_columns ||= exported_columns.map(&:column)
|
|
|
|
@template_exported_columns.include?(exported_column.column)
|
|
|
|
end
|
|
|
|
|
2024-07-23 17:05:16 +02:00
|
|
|
private
|
2024-03-02 22:13:09 +01:00
|
|
|
|
2024-07-23 17:05:16 +02:00
|
|
|
def ensure_pjs_are_legit
|
|
|
|
legitimate_pj_stable_ids = procedure.exportables_pieces_jointes_for_all_versions.map(&:stable_id)
|
2024-03-02 22:13:09 +01:00
|
|
|
|
2024-07-23 17:05:16 +02:00
|
|
|
self.pjs = pjs.filter { _1.stable_id.in?(legitimate_pj_stable_ids) }
|
2024-03-02 22:13:09 +01:00
|
|
|
end
|
|
|
|
end
|