diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index a93320573..02d8c0b4f 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -103,11 +103,13 @@ class Users::DossiersController < UsersController def siret_informations @facade = facade params[:dossier_id] - update_current_user_siret! siret + update_current_user_siret!(siret) - dossier = DossierService.new(@facade.dossier, siret, current_user.france_connect_information).dossier_informations! + etablissement_attributes = SIRETService.fetch(siret, @facade.dossier) - if dossier.entreprise.nil? || dossier.etablissement.nil? + if etablissement_attributes.present? && @facade.dossier.create_etablissement(etablissement_attributes) + @facade.dossier.mandataire_social!(current_user.france_connect_information) + else return errors_valid_siret end diff --git a/app/lib/siade/entreprise_adapter.rb b/app/lib/siade/entreprise_adapter.rb index 97723db98..b86833225 100644 --- a/app/lib/siade/entreprise_adapter.rb +++ b/app/lib/siade/entreprise_adapter.rb @@ -9,19 +9,22 @@ class SIADE::EntrepriseAdapter @data_source = nil end + def success? + data_source + rescue + false + end + def to_params - params = {} - - data_source[:entreprise].each do |k, v| - params[k] = v if attr_to_fetch.include?(k) - end + params = data_source[:entreprise].slice(*attr_to_fetch) params[:date_creation] = Time.at(params[:date_creation]).to_datetime - params rescue nil end + private + def attr_to_fetch [ :siren, @@ -29,6 +32,7 @@ class SIADE::EntrepriseAdapter :numero_tva_intracommunautaire, :forme_juridique, :forme_juridique_code, + :mandataires_sociaux, :nom_commercial, :raison_sociale, :siret_siege_social, @@ -38,10 +42,4 @@ class SIADE::EntrepriseAdapter :prenom ] end - - def mandataires_sociaux - data_source[:entreprise][:mandataires_sociaux] - rescue - nil - end end diff --git a/app/lib/siade/etablissement_adapter.rb b/app/lib/siade/etablissement_adapter.rb index 6b52d6bda..ff8c74812 100644 --- a/app/lib/siade/etablissement_adapter.rb +++ b/app/lib/siade/etablissement_adapter.rb @@ -7,23 +7,27 @@ class SIADE::EtablissementAdapter @data_source ||= JSON.parse(SIADE::API.etablissement(@siret), symbolize_names: true) end - def to_params - params = {} + def success? + data_source + rescue => e + false + end - data_source[:etablissement].each do |k, v| - params[k] = v if attr_to_fetch.include?(k) - end - params[:adresse] = adresse - data_source[:etablissement][:adresse].each do |k, v| - params[k] = v if address_attribut_to_fetch.include?(k) - end + def to_params + params = data_source[:etablissement].slice(*attr_to_fetch) + adresse_line = params[:adresse].slice(*address_lines_to_fetch).values.compact.join("\r\n") + params.merge!(params[:adresse].slice(*address_attr_to_fetch)) + params[:adresse] = adresse_line params rescue nil end + private + def attr_to_fetch [ + :adresse, :siret, :siege_social, :naf, @@ -31,13 +35,7 @@ class SIADE::EtablissementAdapter ] end - def adresse - [:l1, :l2, :l3, :l4, :l5, :l6, :l7].map do |line| - data_source[:etablissement][:adresse][line] - end.compact.join("\r\n") - end - - def address_attribut_to_fetch + def address_attr_to_fetch [ :numero_voie, :type_voie, @@ -48,4 +46,8 @@ class SIADE::EtablissementAdapter :code_insee_localite ] end + + def address_lines_to_fetch + [:l1, :l2, :l3, :l4, :l5, :l6, :l7] + end end diff --git a/app/lib/siade/exercices_adapter.rb b/app/lib/siade/exercices_adapter.rb index 59e2af174..db8193829 100644 --- a/app/lib/siade/exercices_adapter.rb +++ b/app/lib/siade/exercices_adapter.rb @@ -11,13 +11,15 @@ class SIADE::ExercicesAdapter def to_params data_source[:exercices].map do |exercice| - { - ca: exercice[:ca], - dateFinExercice: exercice[:date_fin_exercice], - date_fin_exercice_timestamp: exercice[:date_fin_exercice_timestamp] - } + exercice.slice(*attr_to_fetch) end rescue - nil + [] + end + + private + + def attr_to_fetch + [:ca, :date_fin_exercice, :date_fin_exercice_timestamp] end end diff --git a/app/lib/siade/rna_adapter.rb b/app/lib/siade/rna_adapter.rb index fd4686eee..2a3b1c94d 100644 --- a/app/lib/siade/rna_adapter.rb +++ b/app/lib/siade/rna_adapter.rb @@ -8,23 +8,17 @@ class SIADE::RNAAdapter end def to_params - params = {} - - data_source[:association].each do |k, v| - params[k] = v if attr_to_fetch.include?(k) - end - - params[:association_id] = params[:id] - params.delete(:id) - + params = data_source[:association].slice(*attr_to_fetch) + params[:rna] = data_source[:association][:id] params rescue nil end + private + def attr_to_fetch [ - :id, :titre, :objet, :date_creation, diff --git a/app/models/dossier.rb b/app/models/dossier.rb index fe9da1f7a..cf266f3d8 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -293,6 +293,12 @@ class Dossier < ActiveRecord::Base end end + def mandataire_social!(france_connect_information) + if etablissement.mandataire_social?(france_connect_information) + update_column(:mandataire_social, true) + end + end + private def update_state_dates diff --git a/app/models/entreprise.rb b/app/models/entreprise.rb index 5e24a31bb..3e92635c9 100644 --- a/app/models/entreprise.rb +++ b/app/models/entreprise.rb @@ -6,9 +6,13 @@ class Entreprise < ActiveRecord::Base validates_presence_of :siren validates_uniqueness_of :dossier_id + accepts_nested_attributes_for :rna_information + before_save :default_values def default_values self.raison_sociale ||= '' end + + attr_writer :mandataires_sociaux end diff --git a/app/models/etablissement.rb b/app/models/etablissement.rb index 23fc3b0c0..2b51f17ea 100644 --- a/app/models/etablissement.rb +++ b/app/models/etablissement.rb @@ -4,6 +4,9 @@ class Etablissement < ActiveRecord::Base has_many :exercices, dependent: :destroy + accepts_nested_attributes_for :exercices + accepts_nested_attributes_for :entreprise + validates_uniqueness_of :dossier_id def geo_adresse @@ -14,4 +17,14 @@ class Etablissement < ActiveRecord::Base # squeeze needed because of space in excess in the data "#{numero_voie} #{type_voie} #{nom_voie}, #{complement_adresse}, #{code_postal} #{localite}".squeeze(' ') end + + attr_accessor :entreprise_mandataires_sociaux + + def mandataire_social?(france_connect_information) + if france_connect_information.present? + entreprise_mandataires_sociaux&.find do |mandataire| + france_connect_information.mandataire_social?(mandataire) + end + end + end end diff --git a/app/models/exercice.rb b/app/models/exercice.rb index 6b002c092..7336d3f38 100644 --- a/app/models/exercice.rb +++ b/app/models/exercice.rb @@ -2,4 +2,8 @@ class Exercice < ActiveRecord::Base belongs_to :etablissement validates :ca, presence: true, allow_blank: false, allow_nil: false + + def date_fin_exercice + super || dateFinExercice + end end diff --git a/app/models/france_connect_information.rb b/app/models/france_connect_information.rb index 76e804399..a4cdcad85 100644 --- a/app/models/france_connect_information.rb +++ b/app/models/france_connect_information.rb @@ -2,4 +2,10 @@ class FranceConnectInformation < ActiveRecord::Base belongs_to :user validates :france_connect_particulier_id, presence: true, allow_blank: false, allow_nil: false + + def mandataire_social?(params) + params[:nom].casecmp(family_name).zero? && + params[:prenom].casecmp(given_name).zero? && + params[:date_naissance_timestamp] == birthdate.to_time.to_i + end end diff --git a/app/models/rna_information.rb b/app/models/rna_information.rb index 6b7d4f898..409f7bd10 100644 --- a/app/models/rna_information.rb +++ b/app/models/rna_information.rb @@ -2,4 +2,8 @@ class RNAInformation < ActiveRecord::Base belongs_to :entreprise validates :association_id, presence: true, allow_blank: false, allow_nil: false + + def rna=(id) + write_attribute(:association_id, id) + end end diff --git a/app/services/dossier_service.rb b/app/services/dossier_service.rb deleted file mode 100644 index b43015a47..000000000 --- a/app/services/dossier_service.rb +++ /dev/null @@ -1,54 +0,0 @@ -class DossierService - def initialize dossier, siret, france_connect_information - @dossier = dossier - @siret = siret - @france_connect_information = france_connect_information - end - - def dossier_informations! - @entreprise_adapter = SIADE::EntrepriseAdapter.new(DossierService.siren @siret) - - if @entreprise_adapter.to_params.nil? - raise RestClient::ResourceNotFound - end - - @etablissement_adapter = SIADE::EtablissementAdapter.new(@siret) - - if @etablissement_adapter.to_params.nil? - raise RestClient::ResourceNotFound - end - - @dossier.create_entreprise(@entreprise_adapter.to_params) - @dossier.create_etablissement(@etablissement_adapter.to_params) - - @rna_adapter = SIADE::RNAAdapter.new(@siret) - @dossier.entreprise.create_rna_information(@rna_adapter.to_params) - - @exercices_adapter = SIADE::ExercicesAdapter.new(@siret) - @dossier.etablissement.exercices.create(@exercices_adapter.to_params) - - @dossier.update_attributes(mandataire_social: mandataire_social?(@entreprise_adapter.mandataires_sociaux)) - @dossier.etablissement.update_attributes(entreprise: @dossier.entreprise) - - @dossier - end - - def self.siren siret - siret[0..8] - end - - private - - def mandataire_social? mandataires_list - if @france_connect_information.present? - - mandataires_list.each do |mandataire| - return true if mandataire[:nom].casecmp(@france_connect_information.family_name).zero? && - mandataire[:prenom].casecmp(@france_connect_information.given_name).zero? && - mandataire[:date_naissance_timestamp] == @france_connect_information.birthdate.to_time.to_i - end - end - - false - end -end diff --git a/app/services/siret_service.rb b/app/services/siret_service.rb new file mode 100644 index 000000000..f4df8856b --- /dev/null +++ b/app/services/siret_service.rb @@ -0,0 +1,33 @@ +class SIRETService + def self.fetch(siret, dossier = nil) + etablissement = SIADE::EtablissementAdapter.new(siret) + entreprise = SIADE::EntrepriseAdapter.new(siren(siret)) + + if etablissement.success? && entreprise.success? + association = SIADE::RNAAdapter.new(siret) + exercices = SIADE::ExercicesAdapter.new(siret) + + params = etablissement.to_params + .merge(entreprise.to_params.map { |k,v| ["entreprise_#{k}", v] }.to_h) + .merge(association.to_params&.map { |k,v| ["association_#{k}", v] }.to_h) + .merge(exercices_attributes: exercices.to_params) + + # This is to fill legacy models and relationships + if dossier.present? + return params.merge( + entreprise_attributes: entreprise.to_params + .merge({ + dossier: dossier, + rna_information_attributes: association.to_params + }.compact) + ) + else + return params + end + end + end + + def self.siren(siret) + siret[0..8] + end +end diff --git a/app/views/dossiers/_infos_entreprise.html.haml b/app/views/dossiers/_infos_entreprise.html.haml index 6938e2163..aad89acb7 100644 --- a/app/views/dossiers/_infos_entreprise.html.haml +++ b/app/views/dossiers/_infos_entreprise.html.haml @@ -49,7 +49,7 @@ .col-xs-8.entreprise-info - @facade.etablissement.exercices.each_with_index do |exercice, index| %strong - = "#{exercice.dateFinExercice.year} : " + = "#{exercice.date_fin_exercice.year} : " = number_to_currency(exercice.ca) %br diff --git a/app/views/new_gestionnaire/dossiers/_identite_entreprise.html.haml b/app/views/new_gestionnaire/dossiers/_identite_entreprise.html.haml index a60448c29..dd7f1731b 100644 --- a/app/views/new_gestionnaire/dossiers/_identite_entreprise.html.haml +++ b/app/views/new_gestionnaire/dossiers/_identite_entreprise.html.haml @@ -44,7 +44,7 @@ %th Exercices : %td - etablissement.exercices.each_with_index do |exercice, index| - = "#{exercice.dateFinExercice.year} : " + = "#{exercice.date_fin_exercice.year} : " = number_to_currency(exercice.ca) %br - if entreprise.rna_information.present? diff --git a/db/migrate/20180221104139_add_entreprise_to_etablissement.rb b/db/migrate/20180221104139_add_entreprise_to_etablissement.rb new file mode 100644 index 000000000..81889d58c --- /dev/null +++ b/db/migrate/20180221104139_add_entreprise_to_etablissement.rb @@ -0,0 +1,26 @@ +class AddEntrepriseToEtablissement < ActiveRecord::Migration[5.2] + def change + add_column :etablissements, :entreprise_siren, :string + add_column :etablissements, :entreprise_capital_social, :integer + add_column :etablissements, :entreprise_numero_tva_intracommunautaire, :string + add_column :etablissements, :entreprise_forme_juridique, :string + add_column :etablissements, :entreprise_forme_juridique_code, :string + add_column :etablissements, :entreprise_nom_commercial, :string + add_column :etablissements, :entreprise_raison_sociale, :string + add_column :etablissements, :entreprise_siret_siege_social, :string + add_column :etablissements, :entreprise_code_effectif_entreprise, :string + add_column :etablissements, :entreprise_date_creation, :date + add_column :etablissements, :entreprise_nom, :string + add_column :etablissements, :entreprise_prenom, :string + + add_column :etablissements, :association_rna, :string + add_column :etablissements, :association_titre, :string + add_column :etablissements, :association_objet, :text + add_column :etablissements, :association_date_creation, :date + add_column :etablissements, :association_date_declaration, :date + add_column :etablissements, :association_date_publication, :date + + add_column :champs, :etablissement_id, :integer, index: true + add_column :exercices, :date_fin_exercice, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index c85827d5a..d7e76bbf8 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -161,6 +161,7 @@ ActiveRecord::Schema.define(version: 2018_02_26_162351) do t.datetime "created_at" t.datetime "updated_at" t.boolean "private" + t.integer "etablissement_id" t.index ["dossier_id"], name: "index_champs_on_dossier_id" t.index ["private"], name: "index_champs_on_private" t.index ["type_de_champ_id"], name: "index_champs_on_type_de_champ_id" @@ -264,6 +265,24 @@ ActiveRecord::Schema.define(version: 2018_02_26_162351) do t.string "code_insee_localite" t.integer "dossier_id" t.integer "entreprise_id" + t.string "entreprise_siren" + t.integer "entreprise_capital_social" + t.string "entreprise_numero_tva_intracommunautaire" + t.string "entreprise_forme_juridique" + t.string "entreprise_forme_juridique_code" + t.string "entreprise_nom_commercial" + t.string "entreprise_raison_sociale" + t.string "entreprise_siret_siege_social" + t.string "entreprise_code_effectif_entreprise" + t.date "entreprise_date_creation" + t.string "entreprise_nom" + t.string "entreprise_prenom" + t.string "association_rna" + t.string "association_titre" + t.text "association_objet" + t.date "association_date_creation" + t.date "association_date_declaration" + t.date "association_date_publication" t.index ["dossier_id"], name: "index_etablissements_on_dossier_id" end @@ -272,6 +291,7 @@ ActiveRecord::Schema.define(version: 2018_02_26_162351) do t.datetime "dateFinExercice" t.integer "date_fin_exercice_timestamp" t.integer "etablissement_id" + t.datetime "date_fin_exercice" end create_table "follows", id: :serial, force: :cascade do |t| diff --git a/spec/factories/exercice.rb b/spec/factories/exercice.rb index b9c6f38cc..fedc9ce47 100644 --- a/spec/factories/exercice.rb +++ b/spec/factories/exercice.rb @@ -1,7 +1,7 @@ FactoryBot.define do factory :exercice do ca '12345678' - dateFinExercice "2014-12-30 23:00:00" + date_fin_exercice "2014-12-30 23:00:00" date_fin_exercice_timestamp 1419980400 association :etablissement, factory: [:etablissement] end diff --git a/spec/lib/siade/entreprise_adapter_spec.rb b/spec/lib/siade/entreprise_adapter_spec.rb index 9cd14a50f..161aac0a2 100644 --- a/spec/lib/siade/entreprise_adapter_spec.rb +++ b/spec/lib/siade/entreprise_adapter_spec.rb @@ -1,10 +1,12 @@ require 'spec_helper' describe SIADE::EntrepriseAdapter do - subject { described_class.new('418166096').to_params } + let(:siren) { '418166096' } + let(:adapter) { described_class.new(siren) } + subject { adapter.to_params } before do - stub_request(:get, "https://staging.entreprise.api.gouv.fr/v2/entreprises/418166096?token=#{SIADETOKEN}") + stub_request(:get, "https://staging.entreprise.api.gouv.fr/v2/entreprises/#{siren}?token=#{SIADETOKEN}") .to_return(body: File.read('spec/support/files/entreprise.json', status: 200)) end @@ -14,7 +16,7 @@ describe SIADE::EntrepriseAdapter do context 'Attributs Entreprises' do it 'L\'entreprise contient bien un siren' do - expect(subject[:siren]).to eq('418166096') + expect(subject[:siren]).to eq(siren) end it 'L\'entreprise contient bien un capital_social' do @@ -60,10 +62,14 @@ describe SIADE::EntrepriseAdapter do it 'L\'entreprise contient bien un prenom' do expect(subject[:prenom]).to eq('test_prenom') end + + it 'L\'entreprise contient bien les mandataires_sociaux' do + expect(subject[:mandataires_sociaux]).to be_an_instance_of(Array) + end end context 'Mandataire sociaux' do - subject { described_class.new('418166096').mandataires_sociaux } + subject { described_class.new(siren).to_params[:mandataires_sociaux] } it '#to_params class est une Hash ?' do expect(subject).to be_an_instance_of(Array) diff --git a/spec/lib/siade/etablissement_adapter_spec.rb b/spec/lib/siade/etablissement_adapter_spec.rb index 3e5092692..4a328aa03 100644 --- a/spec/lib/siade/etablissement_adapter_spec.rb +++ b/spec/lib/siade/etablissement_adapter_spec.rb @@ -2,7 +2,7 @@ require 'spec_helper' describe SIADE::EtablissementAdapter do context 'SIRET valide' do - let(:siret) { 41_816_609_600_051 } + let(:siret) { '41816609600051' } subject { described_class.new(siret).to_params } before do @@ -16,7 +16,7 @@ describe SIADE::EtablissementAdapter do context 'Attributs Etablissements' do it 'L\'entreprise contient bien un siret' do - expect(subject[:siret]).to eq('41816609600051') + expect(subject[:siret]).to eq(siret) end it 'L\'entreprise contient bien un siege_social' do diff --git a/spec/lib/siade/exercices_adapter_spec.rb b/spec/lib/siade/exercices_adapter_spec.rb index eaab69563..10a244faf 100644 --- a/spec/lib/siade/exercices_adapter_spec.rb +++ b/spec/lib/siade/exercices_adapter_spec.rb @@ -23,7 +23,7 @@ describe SIADE::ExercicesAdapter do end it 'L\'exercice contient bien une date de fin d\'exercice' do - expect(subject[0][:dateFinExercice]).to eq("2013-12-31T00:00:00+01:00") + expect(subject[0][:date_fin_exercice]).to eq("2013-12-31T00:00:00+01:00") end it 'L\'exercice contient bien une date_fin_exercice_timestamp' do diff --git a/spec/lib/siade/rna_adapter_spec.rb b/spec/lib/siade/rna_adapter_spec.rb index 116b14761..dda9c8275 100644 --- a/spec/lib/siade/rna_adapter_spec.rb +++ b/spec/lib/siade/rna_adapter_spec.rb @@ -4,8 +4,9 @@ describe SIADE::RNAAdapter do let(:siret) { '50480511000013' } let(:body) { File.read('spec/support/files/rna.json') } let(:status) { 200 } + let(:adapter) { described_class.new(siret) } - subject { described_class.new(siret).to_params } + subject { adapter.to_params } before do stub_request(:get, /https:\/\/staging.entreprise.api.gouv.fr\/v2\/associations\/.*token=/) @@ -23,7 +24,7 @@ describe SIADE::RNAAdapter do it { expect(subject).to be_an_instance_of(Hash) } describe 'Attributs Associations' do - it { expect(subject[:association_id]).to eq('W595001988') } + it { expect(subject[:rna]).to eq('W595001988') } it { expect(subject[:titre]).to eq('UN SUR QUATRE') }