-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Procedure export dossiers with champs siret
- Loading branch information
Showing
5 changed files
with
271 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,240 @@ | ||
class Procedure::Export | ||
include DossierHelper | ||
|
||
ATTRIBUTES = [ | ||
:id, | ||
:created_at, | ||
:updated_at, | ||
:archived, | ||
:email, | ||
:state, | ||
:initiated_at, | ||
:received_at, | ||
:processed_at, | ||
:motivation, | ||
:emails_instructeurs, | ||
:individual_gender, | ||
:individual_prenom, | ||
:individual_nom, | ||
:individual_birthdate | ||
] | ||
|
||
ETABLISSEMENT_ATTRIBUTES = [ | ||
:siret, | ||
:siege_social, | ||
:naf, | ||
:libelle_naf, | ||
:adresse, | ||
:numero_voie, | ||
:type_voie, | ||
:nom_voie, | ||
:complement_adresse, | ||
:code_postal, | ||
:localite, | ||
:code_insee_localite | ||
] | ||
|
||
ENTREPRISE_ATTRIBUTES = [ | ||
:siren, | ||
:capital_social, | ||
:numero_tva_intracommunautaire, | ||
:forme_juridique, | ||
:forme_juridique_code, | ||
:nom_commercial, | ||
:raison_sociale, | ||
:siret_siege_social, | ||
:code_effectif_entreprise, | ||
:date_creation, | ||
:nom, | ||
:prenom | ||
] | ||
|
||
def initialize(procedure, tables: [], since: nil, limit: nil) | ||
@procedure = procedure | ||
@dossiers = procedure.dossiers.downloadable_sorted | ||
if since | ||
@dossiers = @dossiers.since(since) | ||
end | ||
if limit | ||
@dossiers = @dossiers.limit(limit) | ||
end | ||
@dossiers = @dossiers.to_a | ||
@tables = tables.map(&:to_sym) | ||
end | ||
|
||
def to_csv | ||
SpreadsheetArchitect.to_csv(to_table_data(:dossiers)) | ||
end | ||
|
||
def to_xlsx | ||
package = SpreadsheetArchitect.to_axlsx_package(to_table_data(:dossiers)) | ||
|
||
@tables.reduce(package) do |package, table| | ||
SpreadsheetArchitect.to_axlsx_package(to_table_data(table), package) | ||
end.to_stream.read | ||
end | ||
|
||
def to_ods | ||
spreadsheet = SpreadsheetArchitect.to_rodf_spreadsheet(to_table_data(:dossiers)) | ||
|
||
@tables.reduce(spreadsheet) do |spreadsheet, table| | ||
SpreadsheetArchitect.to_rodf_spreadsheet(to_table_data(table), spreadsheet) | ||
end.to_stream.read | ||
end | ||
|
||
private | ||
|
||
def to_table_data(table) | ||
case table | ||
when :dossiers | ||
dossiers_table_data | ||
when :etablissements | ||
etablissements_table_data | ||
end | ||
end | ||
|
||
def empty_table_data(sheet_name, headers = []) | ||
{ | ||
sheet_name: sheet_name, | ||
headers: headers, | ||
data: [[]] | ||
} | ||
end | ||
|
||
def dossiers_table_data | ||
if @dossiers.any? | ||
{ | ||
sheet_name: 'Dossiers', | ||
headers: dossiers_headers, | ||
data: dossiers_data | ||
} | ||
else | ||
empty_table_data('Dossiers', dossiers_headers) | ||
end | ||
end | ||
|
||
def etablissements_table_data | ||
@etablissements = @dossiers.flat_map do |dossier| | ||
dossier.champs.select do |champ| | ||
champ.is_a?(Champs::SiretChamp) | ||
end + dossier.champs_private.select do |champ| | ||
champ.is_a?(Champs::SiretChamp) | ||
end.map(&:etablissement).compact | ||
end | ||
|
||
if @etablissements.any? | ||
{ | ||
sheet_name: 'Etablissements', | ||
headers: etablissements_headers, | ||
data: etablissements_data | ||
} | ||
else | ||
empty_table_data('Etablissements', etablissements_headers) | ||
end | ||
end | ||
|
||
def dossiers_headers | ||
headers = ATTRIBUTES.map do |key| | ||
label_for_export(key.to_s) | ||
end | ||
headers += @procedure.types_de_champ.map do |champ| | ||
label_for_export(champ.libelle) | ||
end | ||
headers += @procedure.types_de_champ_private.map do |champ| | ||
label_for_export(champ.libelle) | ||
end | ||
headers += ETABLISSEMENT_ATTRIBUTES.map do |key| | ||
label_for_export("etablissement.#{key}") | ||
end | ||
headers += ENTREPRISE_ATTRIBUTES.map do |key| | ||
label_for_export("entreprise.#{key}") | ||
end | ||
headers | ||
end | ||
|
||
def dossiers_data | ||
@dossiers.map do |dossier| | ||
values = ATTRIBUTES.map do |key| | ||
case key | ||
when :email | ||
dossier.user.email | ||
when :state | ||
dossier_legacy_state(dossier) | ||
when :initiated_at | ||
dossier.en_construction_at | ||
when :received_at | ||
dossier.en_instruction_at | ||
when :individual_prenom | ||
dossier.individual&.prenom | ||
when :individual_nom | ||
dossier.individual&.nom | ||
when :individual_birthdate | ||
dossier.individual&.birthdate | ||
when :individual_gender | ||
dossier.individual&.gender | ||
when :emails_instructeurs | ||
dossier.followers_gestionnaires.map(&:email).join(' ') | ||
else | ||
dossier.read_attribute(key) | ||
end.to_s | ||
end | ||
values += dossier.champs.map do |champ| | ||
value_for_export(champ) | ||
end | ||
values += dossier.champs_private.map do |champ| | ||
value_for_export(champ) | ||
end | ||
values += etablissement_data(dossier.etablissement) | ||
values | ||
end | ||
end | ||
|
||
def etablissements_headers | ||
headers = [:dossier_id, :libelle] | ||
headers += ETABLISSEMENT_ATTRIBUTES.map do |key| | ||
label_for_export("etablissement.#{key}") | ||
end | ||
headers += ENTREPRISE_ATTRIBUTES.map do |key| | ||
label_for_export("entreprise.#{key}") | ||
end | ||
headers | ||
end | ||
|
||
def etablissements_data | ||
@etablissements.map do |etablissement| | ||
data = [ | ||
etablissement.champ.dossier_id, | ||
label_for_export(etablissement.champ.libelle) | ||
] | ||
data += etablissement_data(etablissement) | ||
end | ||
end | ||
|
||
def etablissement_data(etablissement) | ||
data = ETABLISSEMENT_ATTRIBUTES.map do |key| | ||
case key | ||
when :adresse | ||
etablissement&.adresse&.chomp&.gsub("\r\n", ' ')&.delete("\r") | ||
else | ||
etablissement&.read_attribute(key) | ||
end | ||
end | ||
data += ENTREPRISE_ATTRIBUTES.map do |key| | ||
case key | ||
when :date_creation | ||
etablissement&.entreprise_date_creation&.to_datetime | ||
else | ||
etablissement&.read_attribute(:"entreprise_#{key}") | ||
end | ||
end | ||
data | ||
end | ||
|
||
def label_for_export(label) | ||
label.parameterize.underscore.to_sym | ||
end | ||
|
||
def value_for_export(champ) | ||
champ.for_export | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters