2018-11-22 00:14:16 +01:00
|
|
|
class ProcedureExportService
|
2019-06-25 15:46:10 +02:00
|
|
|
attr_reader :dossiers
|
2018-11-22 00:14:16 +01:00
|
|
|
|
2019-06-25 15:46:10 +02:00
|
|
|
def initialize(procedure, dossiers)
|
2018-11-22 00:14:16 +01:00
|
|
|
@procedure = procedure
|
2019-09-18 16:51:45 +02:00
|
|
|
@dossiers = dossiers.downloadable_sorted
|
2019-06-25 15:46:10 +02:00
|
|
|
@tables = [:dossiers, :etablissements, :avis] + champs_repetables_options
|
2018-11-22 00:14:16 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_csv
|
2019-06-25 15:46:10 +02:00
|
|
|
SpreadsheetArchitect.to_csv(options_for(:dossiers, :csv))
|
2018-11-22 00:14:16 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_xlsx
|
2019-06-25 15:46:10 +02:00
|
|
|
# We recursively build multi page spreadsheet
|
|
|
|
@tables.reduce(nil) do |package, table|
|
|
|
|
SpreadsheetArchitect.to_axlsx_package(options_for(table, :xlsx), package)
|
2018-11-22 00:14:16 +01:00
|
|
|
end.to_stream.read
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_ods
|
2019-06-25 15:46:10 +02:00
|
|
|
# We recursively build multi page spreadsheet
|
|
|
|
@tables.reduce(nil) do |spreadsheet, table|
|
|
|
|
SpreadsheetArchitect.to_rodf_spreadsheet(options_for(table, :ods), spreadsheet)
|
2018-11-22 00:14:16 +01:00
|
|
|
end.bytes
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-06-25 15:46:10 +02:00
|
|
|
def etablissements
|
|
|
|
@etablissements ||= dossiers.flat_map do |dossier|
|
2018-12-28 17:54:53 +01:00
|
|
|
[dossier.champs, dossier.champs_private]
|
|
|
|
.flatten
|
2019-09-12 11:26:22 +02:00
|
|
|
.filter { |champ| champ.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
|
|
|
|
@champs_repetables ||= dossiers.flat_map do |dossier|
|
|
|
|
[dossier.champs, dossier.champs_private]
|
|
|
|
.flatten
|
|
|
|
.filter { |champ| champ.is_a?(Champs::RepetitionChamp) }
|
|
|
|
end.group_by(&:libelle_for_export)
|
2018-11-22 00:14:16 +01:00
|
|
|
end
|
|
|
|
|
2019-06-25 15:46:10 +02:00
|
|
|
def champs_repetables_options
|
|
|
|
champs_repetables.map do |libelle, champs|
|
2018-12-28 17:54:53 +01:00
|
|
|
[
|
2019-06-25 15:46:10 +02:00
|
|
|
libelle,
|
|
|
|
champs.flat_map(&:rows_for_export)
|
|
|
|
]
|
2018-11-22 00:14:16 +01:00
|
|
|
end
|
|
|
|
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' }
|
|
|
|
when Array
|
|
|
|
{ instances: table.last, sheet_name: table.first }
|
|
|
|
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)
|
|
|
|
types_de_champ = @procedure.types_de_champ_for_export
|
|
|
|
types_de_champ_private = @procedure.types_de_champ_private_for_export
|
|
|
|
|
|
|
|
Proc.new do |instance|
|
|
|
|
instance.send(:"spreadsheet_columns_#{format}", types_de_champ: types_de_champ, types_de_champ_private: types_de_champ_private)
|
|
|
|
end
|
|
|
|
end
|
2018-11-22 00:14:16 +01:00
|
|
|
end
|