2016-03-22 17:36:36 +01:00
|
|
|
|
class PiecesJustificativesService
|
2022-04-04 22:53:06 +02:00
|
|
|
|
def self.liste_documents(dossiers, for_expert)
|
2022-04-05 15:30:23 +02:00
|
|
|
|
bill_ids = []
|
|
|
|
|
|
|
|
|
|
docs = dossiers.in_batches.flat_map do |batch|
|
2022-04-05 15:10:22 +02:00
|
|
|
|
pjs = pjs_for_champs(batch, for_expert) +
|
2022-04-04 23:53:29 +02:00
|
|
|
|
pjs_for_commentaires(batch) +
|
2022-04-05 15:10:22 +02:00
|
|
|
|
pjs_for_dossier(batch)
|
|
|
|
|
|
|
|
|
|
if !for_expert
|
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
|
|
|
|
|
|
|
|
|
if !for_expert
|
|
|
|
|
# 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)
|
|
|
|
|
tdcs = revision.types_de_champ.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)
|
2019-09-12 11:26:22 +02:00
|
|
|
|
dossier.champs.filter { |champ| champ.type_de_champ.old_pj }.map do |champ|
|
2019-01-10 18:46:19 +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
|
|
|
|
|
}
|
|
|
|
|
end
|
|
|
|
|
end
|
2019-09-03 15:17:19 +02:00
|
|
|
|
|
2021-07-01 13:33:22 +02:00
|
|
|
|
def self.clone_attachments(original, kopy)
|
|
|
|
|
if original.is_a?(TypeDeChamp)
|
|
|
|
|
clone_attachment(original.piece_justificative_template, kopy.piece_justificative_template)
|
|
|
|
|
elsif original.is_a?(Procedure)
|
|
|
|
|
clone_attachment(original.logo, kopy.logo)
|
|
|
|
|
clone_attachment(original.notice, kopy.notice)
|
|
|
|
|
clone_attachment(original.deliberation, kopy.deliberation)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.clone_attachment(original_attachment, copy_attachment)
|
|
|
|
|
if original_attachment.attached?
|
|
|
|
|
original_attachment.open do |tempfile|
|
|
|
|
|
copy_attachment.attach({
|
|
|
|
|
io: File.open(tempfile.path),
|
|
|
|
|
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 }
|
|
|
|
|
})
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2021-06-24 21:01:59 +02:00
|
|
|
|
class FakeAttachment < Hashie::Dash
|
|
|
|
|
property :filename
|
|
|
|
|
property :name
|
|
|
|
|
property :file
|
|
|
|
|
property :id
|
|
|
|
|
property :created_at
|
|
|
|
|
|
|
|
|
|
def download
|
2021-06-24 21:00:58 +02:00
|
|
|
|
file.read
|
2021-06-24 21:01:59 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def read(*args)
|
|
|
|
|
file.read(*args)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def close
|
|
|
|
|
file.close
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def attached?
|
|
|
|
|
true
|
|
|
|
|
end
|
2021-08-03 13:17:35 +02:00
|
|
|
|
|
|
|
|
|
def record_type
|
|
|
|
|
'Fake'
|
|
|
|
|
end
|
2021-06-24 21:01:59 +02:00
|
|
|
|
end
|
|
|
|
|
|
2022-04-12 19:20:50 +02:00
|
|
|
|
def self.generate_dossier_export(dossier)
|
|
|
|
|
pdf = ApplicationController
|
|
|
|
|
.render(template: 'dossiers/show', formats: [:pdf],
|
|
|
|
|
assigns: {
|
|
|
|
|
include_infos_administration: true,
|
|
|
|
|
dossier: dossier
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
FakeAttachment.new(
|
|
|
|
|
file: StringIO.new(pdf),
|
|
|
|
|
filename: "export-#{dossier.id}.pdf",
|
|
|
|
|
name: 'pdf_export_for_instructeur',
|
|
|
|
|
id: dossier.id,
|
|
|
|
|
created_at: dossier.updated_at
|
|
|
|
|
)
|
2021-04-29 17:29:47 +02:00
|
|
|
|
end
|
|
|
|
|
|
2021-04-29 16:16:12 +02:00
|
|
|
|
private
|
|
|
|
|
|
2022-04-04 23:08:01 +02:00
|
|
|
|
def self.pjs_for_champs(dossiers, for_expert = false)
|
2022-04-01 15:26:03 +02:00
|
|
|
|
champs = Champ
|
|
|
|
|
.joins(:piece_justificative_file_attachment)
|
2022-04-04 23:08:01 +02:00
|
|
|
|
.where(type: "Champs::PieceJustificativeChamp", dossier: dossiers)
|
2020-06-24 12:56:58 +02:00
|
|
|
|
|
2022-04-01 15:26:03 +02:00
|
|
|
|
if for_expert
|
|
|
|
|
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
|
|
|
|
|
|
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
|
|
|
|
|
.where(dossier: dossiers)
|
|
|
|
|
.pluck(:id, :dossier_id, :bill_signature_id)
|
|
|
|
|
|
|
|
|
|
dol_id_dossier_id = dol_id_dossier_id_bill_id
|
|
|
|
|
.map { |dol_id, dossier_id, _| [dol_id, dossier_id] }
|
|
|
|
|
.to_h
|
2022-04-01 16:21:38 +02:00
|
|
|
|
|
2022-04-04 23:40:20 +02:00
|
|
|
|
bill_ids = dol_id_dossier_id_bill_id.map(&:third).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-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
|
|
|
|
|
.metadata[:virus_scan_result] == ActiveStorage::VirusScanner::SAFE
|
|
|
|
|
end
|
2017-04-04 15:27:04 +02:00
|
|
|
|
end
|