2016-03-22 17:36:36 +01:00
|
|
|
|
class PiecesJustificativesService
|
2023-10-03 17:07:25 +02:00
|
|
|
|
def self.liste_documents(dossiers,
|
|
|
|
|
with_bills:,
|
|
|
|
|
with_champs_private:,
|
|
|
|
|
with_avis_piece_justificative:)
|
2022-04-05 15:30:23 +02:00
|
|
|
|
bill_ids = []
|
|
|
|
|
|
|
|
|
|
docs = dossiers.in_batches.flat_map do |batch|
|
2023-01-30 17:42:03 +01:00
|
|
|
|
pjs = pjs_for_champs(batch, with_champs_private:) +
|
2022-04-04 23:53:29 +02:00
|
|
|
|
pjs_for_commentaires(batch) +
|
2023-10-03 17:07:25 +02:00
|
|
|
|
pjs_for_dossier(batch) +
|
|
|
|
|
pjs_for_avis(batch, with_avis_piece_justificative:)
|
2022-04-05 15:10:22 +02:00
|
|
|
|
|
2023-01-30 17:42:03 +01:00
|
|
|
|
if with_bills
|
2022-04-05 15:30:23 +02:00
|
|
|
|
# some bills are shared among operations
|
|
|
|
|
# so first, all the bill_ids are fetched
|
|
|
|
|
operation_logs, some_bill_ids = operation_logs_and_signature_ids(batch)
|
|
|
|
|
|
|
|
|
|
pjs += operation_logs
|
|
|
|
|
bill_ids += some_bill_ids
|
2022-04-05 15:10:22 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
pjs
|
2022-04-04 23:53:29 +02:00
|
|
|
|
end
|
2022-04-05 15:30:23 +02:00
|
|
|
|
|
2023-01-30 17:42:03 +01:00
|
|
|
|
if with_bills
|
2022-04-05 15:30:23 +02:00
|
|
|
|
# then the bills are retrieved without duplication
|
|
|
|
|
docs += signatures(bill_ids.uniq)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
docs
|
2019-07-01 15:55:37 +02:00
|
|
|
|
end
|
|
|
|
|
|
2020-08-27 19:55:10 +02:00
|
|
|
|
def self.serialize_types_de_champ_as_type_pj(revision)
|
2022-04-28 14:25:49 +02:00
|
|
|
|
tdcs = revision.types_de_champ_public.filter { |type_champ| type_champ.old_pj.present? }
|
2019-01-10 15:55:04 +01:00
|
|
|
|
tdcs.map.with_index do |type_champ, order_place|
|
|
|
|
|
description = type_champ.description
|
|
|
|
|
if /^(?<original_description>.*?)(?:[\r\n]+)Récupérer le formulaire vierge pour mon dossier : (?<lien_demarche>http.*)$/m =~ description
|
|
|
|
|
description = original_description
|
|
|
|
|
end
|
|
|
|
|
{
|
|
|
|
|
id: type_champ.old_pj[:stable_id],
|
|
|
|
|
libelle: type_champ.libelle,
|
|
|
|
|
description: description,
|
|
|
|
|
order_place: order_place,
|
|
|
|
|
lien_demarche: lien_demarche
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
end
|
2019-01-10 18:46:19 +01:00
|
|
|
|
|
|
|
|
|
def self.serialize_champs_as_pjs(dossier)
|
2022-11-10 22:21:14 +01:00
|
|
|
|
dossier.champs_public.filter { |champ| champ.type_de_champ.old_pj }.map do |champ|
|
2022-12-06 14:41:03 +01:00
|
|
|
|
{
|
|
|
|
|
created_at: champ.created_at&.in_time_zone('UTC'),
|
|
|
|
|
type_de_piece_justificative_id: champ.type_de_champ.old_pj[:stable_id],
|
|
|
|
|
content_url: champ.for_api,
|
|
|
|
|
user: champ.dossier.user
|
|
|
|
|
}
|
2022-11-08 10:29:49 +01:00
|
|
|
|
end.flatten
|
2019-01-10 18:46:19 +01:00
|
|
|
|
end
|
2019-09-03 15:17:19 +02:00
|
|
|
|
|
2021-07-01 13:33:22 +02:00
|
|
|
|
def self.clone_attachments(original, kopy)
|
2022-11-10 13:01:15 +01:00
|
|
|
|
case original
|
2022-11-10 10:58:59 +01:00
|
|
|
|
when Champs::PieceJustificativeChamp, Champs::TitreIdentiteChamp
|
2022-11-22 10:04:56 +01:00
|
|
|
|
original.piece_justificative_file.attachments.each do |attachment|
|
|
|
|
|
kopy.piece_justificative_file.attach(attachment.blob)
|
|
|
|
|
end
|
2022-11-10 13:01:15 +01:00
|
|
|
|
when TypeDeChamp
|
2022-12-16 15:43:36 +01:00
|
|
|
|
clone_attachment(original, kopy, :piece_justificative_template)
|
2022-11-10 13:01:15 +01:00
|
|
|
|
when Procedure
|
2022-12-16 15:43:36 +01:00
|
|
|
|
clone_attachment(original, kopy, :logo)
|
|
|
|
|
clone_attachment(original, kopy, :notice)
|
|
|
|
|
clone_attachment(original, kopy, :deliberation)
|
2022-11-10 13:01:15 +01:00
|
|
|
|
when AttestationTemplate
|
2022-12-16 15:43:36 +01:00
|
|
|
|
clone_attachment(original, kopy, :logo)
|
|
|
|
|
clone_attachment(original, kopy, :signature)
|
|
|
|
|
when Etablissement
|
|
|
|
|
clone_attachment(original, kopy, :entreprise_attestation_sociale)
|
|
|
|
|
clone_attachment(original, kopy, :entreprise_attestation_fiscale)
|
2021-07-01 13:33:22 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2022-12-16 15:43:36 +01:00
|
|
|
|
def self.clone_attachment(original, kopy, attachment_name)
|
|
|
|
|
attachment = original.public_send(attachment_name)
|
|
|
|
|
if attachment.attached?
|
|
|
|
|
kopy.public_send(attachment_name).attach(attachment.blob)
|
2021-07-01 13:33:22 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2023-03-28 16:56:02 +02:00
|
|
|
|
def self.generate_dossier_export(dossiers, include_infos_administration: false, include_avis_for_expert: false)
|
2022-04-06 00:20:52 +02:00
|
|
|
|
return [] if dossiers.empty?
|
|
|
|
|
|
2022-04-07 15:23:18 +02:00
|
|
|
|
pdfs = []
|
|
|
|
|
|
2022-04-06 00:20:52 +02:00
|
|
|
|
procedure = dossiers.first.procedure
|
2022-07-21 13:05:30 +02:00
|
|
|
|
dossiers = dossiers.includes(:individual, :traitement, :etablissement, user: :france_connect_information, avis: :expert, commentaires: [:instructeur, :expert])
|
|
|
|
|
dossiers = DossierPreloader.new(dossiers).in_batches
|
|
|
|
|
dossiers.each do |dossier|
|
|
|
|
|
dossier.association(:procedure).target = procedure
|
|
|
|
|
|
2022-04-07 15:23:18 +02:00
|
|
|
|
pdf = ApplicationController
|
|
|
|
|
.render(template: 'dossiers/show', formats: [:pdf],
|
|
|
|
|
assigns: {
|
2023-01-18 11:42:25 +01:00
|
|
|
|
include_infos_administration:,
|
2023-03-28 16:56:02 +02:00
|
|
|
|
include_avis_for_expert:,
|
2022-07-21 13:05:30 +02:00
|
|
|
|
dossier: dossier
|
2022-04-07 15:23:18 +02:00
|
|
|
|
})
|
|
|
|
|
|
2022-11-18 11:30:21 +01:00
|
|
|
|
a = ActiveStorage::FakeAttachment.new(
|
2022-04-07 15:23:18 +02:00
|
|
|
|
file: StringIO.new(pdf),
|
|
|
|
|
filename: "export-#{dossier.id}.pdf",
|
|
|
|
|
name: 'pdf_export_for_instructeur',
|
|
|
|
|
id: dossier.id,
|
|
|
|
|
created_at: dossier.updated_at
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
pdfs << ActiveStorage::DownloadableFile.pj_and_path(dossier.id, a)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
pdfs
|
2021-04-29 17:29:47 +02:00
|
|
|
|
end
|
|
|
|
|
|
2021-04-29 16:16:12 +02:00
|
|
|
|
private
|
|
|
|
|
|
2023-01-30 17:42:03 +01:00
|
|
|
|
def self.pjs_for_champs(dossiers, with_champs_private:)
|
2022-04-01 15:26:03 +02:00
|
|
|
|
champs = Champ
|
2022-10-20 10:14:47 +02:00
|
|
|
|
.joins(:piece_justificative_file_attachments)
|
2022-04-04 23:08:01 +02:00
|
|
|
|
.where(type: "Champs::PieceJustificativeChamp", dossier: dossiers)
|
2020-06-24 12:56:58 +02:00
|
|
|
|
|
2023-01-30 17:42:03 +01:00
|
|
|
|
if !with_champs_private
|
2022-04-01 15:26:03 +02:00
|
|
|
|
champs = champs.where(private: false)
|
|
|
|
|
end
|
2020-06-24 12:56:58 +02:00
|
|
|
|
|
2022-04-04 23:08:01 +02:00
|
|
|
|
champ_id_dossier_id = champs
|
|
|
|
|
.pluck(:id, :dossier_id)
|
|
|
|
|
.to_h
|
|
|
|
|
|
2022-04-01 15:26:03 +02:00
|
|
|
|
ActiveStorage::Attachment
|
|
|
|
|
.includes(:blob)
|
2022-04-04 23:08:01 +02:00
|
|
|
|
.where(record_type: "Champ", record_id: champ_id_dossier_id.keys)
|
2022-04-08 14:45:51 +02:00
|
|
|
|
.filter { |a| safe_attachment(a) }
|
2022-04-04 23:08:01 +02:00
|
|
|
|
.map do |a|
|
|
|
|
|
dossier_id = champ_id_dossier_id[a.record_id]
|
|
|
|
|
ActiveStorage::DownloadableFile.pj_and_path(dossier_id, a)
|
|
|
|
|
end
|
2019-09-03 15:17:19 +02:00
|
|
|
|
end
|
2020-06-24 12:56:58 +02:00
|
|
|
|
|
2022-04-04 23:15:10 +02:00
|
|
|
|
def self.pjs_for_commentaires(dossiers)
|
|
|
|
|
commentaire_id_dossier_id = Commentaire
|
2022-04-01 15:22:19 +02:00
|
|
|
|
.joins(:piece_jointe_attachment)
|
2022-04-04 23:15:10 +02:00
|
|
|
|
.where(dossier: dossiers)
|
|
|
|
|
.pluck(:id, :dossier_id)
|
|
|
|
|
.to_h
|
2022-04-01 15:22:19 +02:00
|
|
|
|
|
|
|
|
|
ActiveStorage::Attachment
|
|
|
|
|
.includes(:blob)
|
2022-04-04 23:15:10 +02:00
|
|
|
|
.where(record_type: "Commentaire", record_id: commentaire_id_dossier_id.keys)
|
2022-04-08 14:45:51 +02:00
|
|
|
|
.filter { |a| safe_attachment(a) }
|
2022-04-04 23:15:10 +02:00
|
|
|
|
.map do |a|
|
|
|
|
|
dossier_id = commentaire_id_dossier_id[a.record_id]
|
|
|
|
|
ActiveStorage::DownloadableFile.pj_and_path(dossier_id, a)
|
|
|
|
|
end
|
2020-06-24 12:56:58 +02:00
|
|
|
|
end
|
2020-12-08 16:34:38 +01:00
|
|
|
|
|
2022-04-05 15:10:22 +02:00
|
|
|
|
def self.pjs_for_dossier(dossiers)
|
|
|
|
|
motivations(dossiers) +
|
2022-04-04 23:40:20 +02:00
|
|
|
|
attestations(dossiers) +
|
|
|
|
|
etablissements(dossiers)
|
2020-12-08 16:34:38 +01:00
|
|
|
|
end
|
2022-04-01 16:21:38 +02:00
|
|
|
|
|
2022-04-04 23:40:20 +02:00
|
|
|
|
def self.etablissements(dossiers)
|
|
|
|
|
etablissement_id_dossier_id = Etablissement
|
|
|
|
|
.where(dossier: dossiers)
|
|
|
|
|
.pluck(:id, :dossier_id)
|
|
|
|
|
.to_h
|
2022-04-04 14:40:49 +02:00
|
|
|
|
|
|
|
|
|
ActiveStorage::Attachment
|
|
|
|
|
.includes(:blob)
|
2022-04-04 23:40:20 +02:00
|
|
|
|
.where(record_type: "Etablissement", record_id: etablissement_id_dossier_id.keys)
|
|
|
|
|
.map do |a|
|
|
|
|
|
dossier_id = etablissement_id_dossier_id[a.record_id]
|
|
|
|
|
ActiveStorage::DownloadableFile.pj_and_path(dossier_id, a)
|
|
|
|
|
end
|
2022-04-04 14:40:49 +02:00
|
|
|
|
end
|
|
|
|
|
|
2022-04-04 23:40:20 +02:00
|
|
|
|
def self.motivations(dossiers)
|
2022-04-04 14:40:49 +02:00
|
|
|
|
ActiveStorage::Attachment
|
|
|
|
|
.includes(:blob)
|
2022-04-04 23:40:20 +02:00
|
|
|
|
.where(record_type: "Dossier", name: "justificatif_motivation", record_id: dossiers)
|
2022-04-08 14:45:51 +02:00
|
|
|
|
.filter { |a| safe_attachment(a) }
|
2022-04-04 23:40:20 +02:00
|
|
|
|
.map do |a|
|
|
|
|
|
dossier_id = a.record_id
|
|
|
|
|
ActiveStorage::DownloadableFile.pj_and_path(dossier_id, a)
|
|
|
|
|
end
|
2022-04-04 14:40:49 +02:00
|
|
|
|
end
|
|
|
|
|
|
2022-04-04 23:40:20 +02:00
|
|
|
|
def self.attestations(dossiers)
|
|
|
|
|
attestation_id_dossier_id = Attestation
|
2022-04-04 14:40:49 +02:00
|
|
|
|
.joins(:pdf_attachment)
|
2022-04-04 23:40:20 +02:00
|
|
|
|
.where(dossier: dossiers)
|
|
|
|
|
.pluck(:id, :dossier_id)
|
|
|
|
|
.to_h
|
2022-04-04 14:40:49 +02:00
|
|
|
|
|
|
|
|
|
ActiveStorage::Attachment
|
|
|
|
|
.includes(:blob)
|
2022-04-04 23:40:20 +02:00
|
|
|
|
.where(record_type: "Attestation", record_id: attestation_id_dossier_id.keys)
|
|
|
|
|
.map do |a|
|
|
|
|
|
dossier_id = attestation_id_dossier_id[a.record_id]
|
|
|
|
|
ActiveStorage::DownloadableFile.pj_and_path(dossier_id, a)
|
|
|
|
|
end
|
2022-04-04 14:40:49 +02:00
|
|
|
|
end
|
|
|
|
|
|
2023-10-03 17:07:25 +02:00
|
|
|
|
def self.pjs_for_avis(dossiers, with_avis_piece_justificative:)
|
|
|
|
|
avis_ids_dossier_id_query = Avis.joins(:dossier)
|
|
|
|
|
.where(dossier: dossiers)
|
|
|
|
|
avis_ids_dossier_id_query = avis_ids_dossier_id_query.where(confidentiel: false) if !with_avis_piece_justificative
|
|
|
|
|
avis_ids_dossier_id = avis_ids_dossier_id_query.pluck(:id, :dossier_id).to_h
|
|
|
|
|
|
|
|
|
|
ActiveStorage::Attachment
|
|
|
|
|
.includes(:blob)
|
|
|
|
|
.where(record_type: "Avis", name: "piece_justificative_file", record_id: avis_ids_dossier_id.keys)
|
|
|
|
|
.filter { |a| safe_attachment(a) }
|
|
|
|
|
.map do |a|
|
|
|
|
|
dossier_id = avis_ids_dossier_id[a.record_id]
|
|
|
|
|
ActiveStorage::DownloadableFile.pj_and_path(dossier_id, a)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2022-04-05 15:30:23 +02:00
|
|
|
|
def self.operation_logs_and_signature_ids(dossiers)
|
2022-04-04 23:40:20 +02:00
|
|
|
|
dol_id_dossier_id_bill_id = DossierOperationLog
|
2022-11-18 11:31:23 +01:00
|
|
|
|
.where(dossier: dossiers, data: nil)
|
|
|
|
|
.pluck(:bill_signature_id, :id, :dossier_id)
|
|
|
|
|
dol_id_data_bill_id = DossierOperationLog
|
2022-04-04 23:40:20 +02:00
|
|
|
|
.where(dossier: dossiers)
|
2022-11-18 11:31:23 +01:00
|
|
|
|
.with_data
|
|
|
|
|
.pluck(:bill_signature_id, :id, :dossier_id, :data, :digest, :created_at)
|
2022-04-04 23:40:20 +02:00
|
|
|
|
|
|
|
|
|
dol_id_dossier_id = dol_id_dossier_id_bill_id
|
2022-11-18 11:31:23 +01:00
|
|
|
|
.map { |_, dol_id, dossier_id| [dol_id, dossier_id] }
|
2022-04-04 23:40:20 +02:00
|
|
|
|
.to_h
|
2022-04-01 16:21:38 +02:00
|
|
|
|
|
2022-11-18 11:31:23 +01:00
|
|
|
|
bill_ids = (dol_id_dossier_id_bill_id + dol_id_data_bill_id)
|
|
|
|
|
.map(&:first)
|
|
|
|
|
.uniq
|
|
|
|
|
.compact
|
2022-04-01 18:01:48 +02:00
|
|
|
|
|
|
|
|
|
serialized_dols = ActiveStorage::Attachment
|
|
|
|
|
.includes(:blob)
|
2022-04-04 23:40:20 +02:00
|
|
|
|
.where(record_type: "DossierOperationLog", record_id: dol_id_dossier_id.keys)
|
|
|
|
|
.map do |a|
|
|
|
|
|
dossier_id = dol_id_dossier_id[a.record_id]
|
|
|
|
|
ActiveStorage::DownloadableFile.pj_and_path(dossier_id, a)
|
|
|
|
|
end
|
2022-11-18 11:31:23 +01:00
|
|
|
|
serialized_dols += dol_id_data_bill_id.map do |_, id, dossier_id, data, digest, created_at|
|
|
|
|
|
a = ActiveStorage::FakeAttachment.new(
|
|
|
|
|
file: StringIO.new(data.to_json),
|
|
|
|
|
filename: "operation-#{digest}.json",
|
|
|
|
|
name: 'serialized',
|
|
|
|
|
id: id,
|
|
|
|
|
created_at: created_at,
|
|
|
|
|
record_type: 'DossierOperationLog'
|
|
|
|
|
)
|
|
|
|
|
ActiveStorage::DownloadableFile.pj_and_path(dossier_id, a)
|
|
|
|
|
end
|
2022-04-01 18:01:48 +02:00
|
|
|
|
|
2022-04-05 15:30:23 +02:00
|
|
|
|
[serialized_dols, bill_ids]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.signatures(bill_ids)
|
|
|
|
|
ActiveStorage::Attachment
|
2022-04-01 18:01:48 +02:00
|
|
|
|
.includes(:blob)
|
|
|
|
|
.where(record_type: "BillSignature", record_id: bill_ids)
|
2022-04-04 23:40:20 +02:00
|
|
|
|
.map { |bill| ActiveStorage::DownloadableFile.bill_and_path(bill) }
|
2022-04-01 16:21:38 +02:00
|
|
|
|
end
|
2022-04-08 14:45:51 +02:00
|
|
|
|
|
|
|
|
|
def self.safe_attachment(attachment)
|
|
|
|
|
attachment
|
|
|
|
|
.blob
|
2023-01-06 10:54:27 +01:00
|
|
|
|
.virus_scan_result == ActiveStorage::VirusScanner::SAFE
|
2022-04-08 14:45:51 +02:00
|
|
|
|
end
|
2017-04-04 15:27:04 +02:00
|
|
|
|
end
|