2020-08-06 16:35:45 +02:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: attestation_templates
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# activated :boolean
|
|
|
|
# body :text
|
|
|
|
# footer :text
|
|
|
|
# title :text
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# procedure_id :integer
|
|
|
|
#
|
2017-06-08 14:16:48 +02:00
|
|
|
class AttestationTemplate < ApplicationRecord
|
|
|
|
include ActionView::Helpers::NumberHelper
|
2018-01-02 15:33:11 +01:00
|
|
|
include TagsSubstitutionConcern
|
2017-06-08 14:16:48 +02:00
|
|
|
|
2022-11-09 12:09:24 +01:00
|
|
|
belongs_to :procedure, inverse_of: :attestation_template
|
2022-02-02 16:10:29 +01:00
|
|
|
has_many :revisions, class_name: 'ProcedureRevision', inverse_of: :attestation_template, dependent: :nullify
|
2017-06-08 14:16:48 +02:00
|
|
|
|
2019-08-28 13:11:58 +02:00
|
|
|
has_one_attached :logo
|
|
|
|
has_one_attached :signature
|
2019-08-27 17:42:52 +02:00
|
|
|
|
2022-11-09 12:10:19 +01:00
|
|
|
validates :title, tags: true, if: -> { procedure.present? }
|
|
|
|
validates :body, tags: true, if: -> { procedure.present? }
|
2017-06-27 13:07:06 +02:00
|
|
|
validates :footer, length: { maximum: 190 }
|
2021-09-14 18:03:40 +02:00
|
|
|
|
|
|
|
FILE_MAX_SIZE = 1.megabytes
|
2021-11-24 10:04:20 +01:00
|
|
|
validates :logo, content_type: ['image/png', 'image/jpg', 'image/jpeg'], size: { less_than: FILE_MAX_SIZE }
|
|
|
|
validates :signature, content_type: ['image/png', 'image/jpg', 'image/jpeg'], size: { less_than: FILE_MAX_SIZE }
|
2020-02-24 11:17:52 +01:00
|
|
|
|
2018-08-28 14:10:55 +02:00
|
|
|
DOSSIER_STATE = Dossier.states.fetch(:accepte)
|
2017-06-08 14:16:48 +02:00
|
|
|
|
2017-06-08 14:04:47 +02:00
|
|
|
def attestation_for(dossier)
|
2019-08-27 17:42:52 +02:00
|
|
|
attestation = Attestation.new(title: replace_tags(title, dossier))
|
2019-08-28 13:11:58 +02:00
|
|
|
attestation.pdf.attach(
|
2019-08-27 17:42:52 +02:00
|
|
|
io: build_pdf(dossier),
|
|
|
|
filename: "attestation-dossier-#{dossier.id}.pdf",
|
|
|
|
content_type: 'application/pdf',
|
|
|
|
# we don't want to run virus scanner on this file
|
|
|
|
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE }
|
|
|
|
)
|
|
|
|
attestation
|
2017-06-08 14:04:47 +02:00
|
|
|
end
|
|
|
|
|
2018-04-06 13:09:37 +02:00
|
|
|
def unspecified_champs_for_dossier(dossier)
|
2022-11-10 22:21:14 +01:00
|
|
|
all_champs_with_libelle_index = (dossier.champs_public + dossier.champs_private).index_by { |champ| "tdc#{champ.stable_id}" }
|
2018-04-06 13:09:37 +02:00
|
|
|
|
2021-06-10 15:24:15 +02:00
|
|
|
used_tags.filter_map do |used_tag|
|
2018-04-06 13:09:37 +02:00
|
|
|
corresponding_champ = all_champs_with_libelle_index[used_tag]
|
|
|
|
|
2021-07-21 10:46:44 +02:00
|
|
|
if corresponding_champ && corresponding_champ.blank?
|
2018-04-06 19:22:42 +02:00
|
|
|
corresponding_champ
|
|
|
|
end
|
2021-06-10 15:24:15 +02:00
|
|
|
end
|
2018-04-06 13:09:37 +02:00
|
|
|
end
|
|
|
|
|
2017-06-08 14:16:48 +02:00
|
|
|
def dup
|
2019-08-27 17:42:52 +02:00
|
|
|
attestation_template = AttestationTemplate.new(title: title, body: body, footer: footer, activated: activated)
|
2022-11-10 13:01:15 +01:00
|
|
|
PiecesJustificativesService.clone_attachments(self, attestation_template)
|
2019-08-27 17:42:52 +02:00
|
|
|
attestation_template
|
|
|
|
end
|
|
|
|
|
|
|
|
def logo_url
|
2019-08-28 13:11:58 +02:00
|
|
|
if logo.attached?
|
|
|
|
Rails.application.routes.url_helpers.url_for(logo)
|
2019-08-27 17:42:52 +02:00
|
|
|
end
|
|
|
|
end
|
2017-06-08 14:16:48 +02:00
|
|
|
|
2019-08-27 17:42:52 +02:00
|
|
|
def signature_url
|
2019-08-28 13:11:58 +02:00
|
|
|
if signature.attached?
|
|
|
|
Rails.application.routes.url_helpers.url_for(signature)
|
2017-06-08 14:16:48 +02:00
|
|
|
end
|
2019-08-27 17:42:52 +02:00
|
|
|
end
|
2017-06-08 14:16:48 +02:00
|
|
|
|
2019-08-28 14:27:41 +02:00
|
|
|
def render_attributes_for(params = {})
|
|
|
|
dossier = params.fetch(:dossier, false)
|
|
|
|
|
|
|
|
{
|
|
|
|
created_at: Time.zone.now,
|
2020-07-29 12:11:30 +02:00
|
|
|
title: dossier ? replace_tags(title, dossier) : params.fetch(:title, title),
|
|
|
|
body: dossier ? replace_tags(body, dossier) : params.fetch(:body, body),
|
2019-08-28 14:27:41 +02:00
|
|
|
footer: params.fetch(:footer, footer),
|
|
|
|
logo: params.fetch(:logo, logo.attached? ? logo : nil),
|
|
|
|
signature: params.fetch(:signature, signature.attached? ? signature : nil)
|
|
|
|
}
|
2019-08-28 16:33:51 +02:00
|
|
|
end
|
|
|
|
|
2022-02-02 16:10:29 +01:00
|
|
|
def procedure
|
2022-11-09 12:09:24 +01:00
|
|
|
super || revisions.last&.procedure
|
2022-02-02 16:10:29 +01:00
|
|
|
end
|
|
|
|
|
2022-02-10 19:42:39 +01:00
|
|
|
def logo_checksum
|
|
|
|
logo.attached? ? logo.checksum : nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def signature_checksum
|
|
|
|
signature.attached? ? signature.checksum : nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def logo_filename
|
|
|
|
logo.attached? ? logo.filename : nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def signature_filename
|
|
|
|
signature.attached? ? signature.filename : nil
|
|
|
|
end
|
|
|
|
|
2017-06-08 14:16:48 +02:00
|
|
|
private
|
|
|
|
|
2018-04-06 13:09:37 +02:00
|
|
|
def used_tags
|
2022-07-07 19:14:27 +02:00
|
|
|
used_tags_for(title) + used_tags_for(body)
|
2018-04-06 13:09:37 +02:00
|
|
|
end
|
|
|
|
|
2017-06-08 14:04:47 +02:00
|
|
|
def build_pdf(dossier)
|
2019-08-28 14:27:41 +02:00
|
|
|
attestation = render_attributes_for(dossier: dossier)
|
2020-08-04 16:53:46 +02:00
|
|
|
attestation_view = ApplicationController.render(
|
2021-11-25 16:26:55 +01:00
|
|
|
template: 'administrateurs/attestation_templates/show',
|
2020-08-04 16:53:46 +02:00
|
|
|
formats: :pdf,
|
|
|
|
assigns: { attestation: attestation }
|
|
|
|
)
|
2017-06-08 14:04:47 +02:00
|
|
|
|
2019-08-27 17:42:52 +02:00
|
|
|
StringIO.new(attestation_view)
|
2017-06-08 14:04:47 +02:00
|
|
|
end
|
2017-06-08 14:16:48 +02:00
|
|
|
end
|