2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-11-22 00:14:16 +01:00
|
|
|
class ProcedureExportService
|
2022-04-08 17:10:46 +02:00
|
|
|
attr_reader :procedure, :dossiers
|
2018-11-22 00:14:16 +01:00
|
|
|
|
2024-03-08 17:14:21 +01:00
|
|
|
def initialize(procedure, dossiers, user_profile, export_template)
|
2018-11-22 00:14:16 +01:00
|
|
|
@procedure = procedure
|
2022-04-21 17:02:23 +02:00
|
|
|
@dossiers = dossiers
|
2024-02-07 17:19:50 +01:00
|
|
|
@user_profile = user_profile
|
2024-03-08 17:14:21 +01:00
|
|
|
@export_template = export_template
|
2018-11-22 00:14:16 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_csv
|
2022-04-21 17:02:23 +02:00
|
|
|
@dossiers = @dossiers.downloadable_sorted_batch
|
2022-04-08 17:12:34 +02:00
|
|
|
io = StringIO.new(SpreadsheetArchitect.to_csv(options_for(:dossiers, :csv)))
|
|
|
|
create_blob(io, :csv)
|
2018-11-22 00:14:16 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_xlsx
|
2022-04-21 17:02:23 +02:00
|
|
|
@dossiers = @dossiers.downloadable_sorted_batch
|
2022-06-13 22:54:57 +02:00
|
|
|
tables = [:dossiers, :etablissements, :avis] + champs_repetables_options
|
|
|
|
|
2019-06-25 15:46:10 +02:00
|
|
|
# We recursively build multi page spreadsheet
|
2022-06-13 22:54:57 +02:00
|
|
|
io = tables.reduce(nil) do |package, table|
|
2019-06-25 15:46:10 +02:00
|
|
|
SpreadsheetArchitect.to_axlsx_package(options_for(table, :xlsx), package)
|
2022-04-08 17:12:34 +02:00
|
|
|
end.to_stream
|
|
|
|
create_blob(io, :xlsx)
|
2018-11-22 00:14:16 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_ods
|
2022-04-21 17:02:23 +02:00
|
|
|
@dossiers = @dossiers.downloadable_sorted_batch
|
2022-06-13 22:54:57 +02:00
|
|
|
tables = [:dossiers, :etablissements, :avis] + champs_repetables_options
|
|
|
|
|
2019-06-25 15:46:10 +02:00
|
|
|
# We recursively build multi page spreadsheet
|
2022-06-13 22:54:57 +02:00
|
|
|
io = StringIO.new(tables.reduce(nil) do |spreadsheet, table|
|
2019-06-25 15:46:10 +02:00
|
|
|
SpreadsheetArchitect.to_rodf_spreadsheet(options_for(table, :ods), spreadsheet)
|
2022-04-08 17:12:34 +02:00
|
|
|
end.bytes)
|
|
|
|
create_blob(io, :ods)
|
|
|
|
end
|
2022-04-08 17:12:53 +02:00
|
|
|
|
|
|
|
def to_zip
|
2024-03-08 17:14:21 +01:00
|
|
|
attachments = ActiveStorage::DownloadableFile.create_list_from_dossiers(dossiers:, user_profile: @user_profile, export_template: @export_template)
|
2022-04-08 17:12:53 +02:00
|
|
|
|
|
|
|
DownloadableFileService.download_and_zip(procedure, attachments, base_filename) do |zip_filepath|
|
|
|
|
ArchiveUploader.new(procedure: procedure, filename: filename(:zip), filepath: zip_filepath).blob
|
|
|
|
end
|
2018-11-22 00:14:16 +01:00
|
|
|
end
|
|
|
|
|
2022-11-16 11:50:19 +01:00
|
|
|
def to_geo_json
|
|
|
|
io = StringIO.new(dossiers.to_feature_collection.to_json)
|
|
|
|
create_blob(io, :json)
|
|
|
|
end
|
|
|
|
|
2018-11-22 00:14:16 +01:00
|
|
|
private
|
|
|
|
|
2022-04-08 17:12:34 +02:00
|
|
|
def create_blob(io, format)
|
|
|
|
ActiveStorage::Blob.create_and_upload!(
|
|
|
|
io: io,
|
|
|
|
filename: filename(format),
|
|
|
|
content_type: content_type(format),
|
|
|
|
identify: false,
|
|
|
|
# We generate the exports ourselves, so they are safe
|
2023-10-10 10:20:47 +02:00
|
|
|
metadata: { virus_scan_result: ActiveStorage::VirusScanner::SAFE, identified: true }
|
2022-04-08 17:12:34 +02:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def base_filename
|
|
|
|
@base_filename ||= "dossiers_#{procedure_identifier}_#{Time.zone.now.strftime('%Y-%m-%d_%H-%M')}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def filename(format)
|
|
|
|
"#{base_filename}.#{format}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def procedure_identifier
|
|
|
|
procedure.path || "procedure-#{procedure.id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def content_type(format)
|
|
|
|
case format
|
|
|
|
when :csv
|
|
|
|
'text/csv'
|
|
|
|
when :xlsx
|
|
|
|
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
|
|
|
|
when :ods
|
|
|
|
'application/vnd.oasis.opendocument.spreadsheet'
|
2022-04-08 17:12:53 +02:00
|
|
|
when :zip
|
|
|
|
'application/zip'
|
2022-11-16 11:50:19 +01:00
|
|
|
when :json
|
|
|
|
'application/json'
|
2022-04-08 17:12:34 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-25 15:46:10 +02:00
|
|
|
def etablissements
|
|
|
|
@etablissements ||= dossiers.flat_map do |dossier|
|
2023-11-28 17:33:29 +01:00
|
|
|
dossier.champs.filter { _1.is_a?(Champs::SiretChamp) }
|
2021-06-10 15:24:15 +02:00
|
|
|
end.filter_map(&:etablissement) + dossiers.filter_map(&:etablissement)
|
2018-11-22 00:14:16 +01:00
|
|
|
end
|
|
|
|
|
2019-06-25 15:46:10 +02:00
|
|
|
def avis
|
|
|
|
@avis ||= dossiers.flat_map(&:avis)
|
2018-11-22 00:14:16 +01:00
|
|
|
end
|
|
|
|
|
2019-06-25 15:46:10 +02:00
|
|
|
def champs_repetables_options
|
2021-10-22 12:18:43 +02:00
|
|
|
champs_by_stable_id = dossiers
|
2023-11-28 17:33:29 +01:00
|
|
|
.flat_map { _1.champs.filter(&:repetition?) }
|
2021-10-22 12:18:43 +02:00
|
|
|
.group_by(&:stable_id)
|
|
|
|
|
2022-05-18 12:37:43 +02:00
|
|
|
procedure
|
|
|
|
.types_de_champ_for_procedure_presentation
|
|
|
|
.repetition
|
|
|
|
.filter_map do |type_de_champ_repetition|
|
|
|
|
types_de_champ = procedure.types_de_champ_for_procedure_presentation(type_de_champ_repetition).to_a
|
2022-06-17 11:33:48 +02:00
|
|
|
rows = champs_by_stable_id.fetch(type_de_champ_repetition.stable_id, []).flat_map(&:rows_for_export)
|
2022-05-18 12:37:43 +02:00
|
|
|
|
2022-06-17 11:33:48 +02:00
|
|
|
if types_de_champ.present? && rows.present?
|
2022-05-18 12:37:43 +02:00
|
|
|
{
|
|
|
|
sheet_name: type_de_champ_repetition.libelle_for_export,
|
2022-06-17 11:33:48 +02:00
|
|
|
instances: rows,
|
2022-05-18 12:37:43 +02:00
|
|
|
spreadsheet_columns: Proc.new { |instance| instance.spreadsheet_columns(types_de_champ) }
|
|
|
|
}
|
|
|
|
end
|
2021-10-22 12:18:43 +02:00
|
|
|
end
|
2018-11-22 00:14:16 +01:00
|
|
|
end
|
|
|
|
|
2019-06-25 15:46:10 +02:00
|
|
|
DEFAULT_STYLES = {
|
|
|
|
header_style: { background_color: "000000", color: "FFFFFF", font_size: 12, bold: true },
|
|
|
|
row_style: { background_color: nil, color: "000000", font_size: 12 }
|
|
|
|
}
|
2018-11-22 00:14:16 +01:00
|
|
|
|
2019-06-25 15:46:10 +02:00
|
|
|
def options_for(table, format)
|
2020-09-24 14:24:53 +02:00
|
|
|
options = case table
|
2019-06-25 15:46:10 +02:00
|
|
|
when :dossiers
|
2020-09-08 15:53:07 +02:00
|
|
|
{ instances: dossiers.to_a, sheet_name: 'Dossiers', spreadsheet_columns: spreadsheet_columns(format) }
|
2019-06-25 15:46:10 +02:00
|
|
|
when :etablissements
|
|
|
|
{ instances: etablissements.to_a, sheet_name: 'Etablissements' }
|
|
|
|
when :avis
|
|
|
|
{ instances: avis.to_a, sheet_name: 'Avis' }
|
2021-10-22 12:18:43 +02:00
|
|
|
when Hash
|
|
|
|
table
|
2019-06-25 15:46:10 +02:00
|
|
|
end.merge(DEFAULT_STYLES)
|
2020-09-24 14:24:53 +02:00
|
|
|
|
2020-09-29 15:56:38 +02:00
|
|
|
# transliterate: convert to ASCII characters
|
2020-09-29 14:01:27 +02:00
|
|
|
# to ensure truncate respects 30 bytes
|
2020-09-29 15:56:38 +02:00
|
|
|
# /\*?[] are invalid Excel worksheet characters
|
2021-01-28 18:43:47 +01:00
|
|
|
options[:sheet_name] = I18n.transliterate(options[:sheet_name], locale: :en)
|
2020-09-29 17:29:36 +02:00
|
|
|
.delete('/\*?[]')
|
|
|
|
.truncate(30, omission: '')
|
2020-09-29 14:01:27 +02:00
|
|
|
|
2020-09-24 14:24:53 +02:00
|
|
|
options
|
2018-11-22 00:14:16 +01:00
|
|
|
end
|
2020-09-08 15:53:07 +02:00
|
|
|
|
|
|
|
def spreadsheet_columns(format)
|
2022-04-08 17:10:46 +02:00
|
|
|
types_de_champ = procedure.types_de_champ_for_procedure_presentation.not_repetition.to_a
|
2020-09-08 15:53:07 +02:00
|
|
|
|
|
|
|
Proc.new do |instance|
|
2021-06-22 10:17:10 +02:00
|
|
|
instance.send(:"spreadsheet_columns_#{format}", types_de_champ: types_de_champ)
|
2020-09-08 15:53:07 +02:00
|
|
|
end
|
|
|
|
end
|
2018-11-22 00:14:16 +01:00
|
|
|
end
|