diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index a52d1c474..feea68f30 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -19,6 +19,17 @@ class Users::DossiersController < UsersController procedure = Procedure.find(params['procedure_id']) @etablissement = Etablissement.new(SIADE::EtablissementAdapter.new(siret).to_params) @entreprise = Entreprise.new(SIADE::EntrepriseAdapter.new(siren).to_params) + + exercices = SIADE::ExercicesAdapter.new(siret).to_params + + unless exercices.nil? + exercices.each_value do |exercice| + exercice = Exercice.new(exercice) + exercice.etablissement = @etablissement + exercice.save + end + end + @dossier = Dossier.create(user: current_user) @dossier.draft! diff --git a/app/models/etablissement.rb b/app/models/etablissement.rb index 66c086661..d05b73b0c 100644 --- a/app/models/etablissement.rb +++ b/app/models/etablissement.rb @@ -1,4 +1,6 @@ class Etablissement < ActiveRecord::Base belongs_to :dossier belongs_to :entreprise + + has_many :exercices end diff --git a/app/models/exercice.rb b/app/models/exercice.rb new file mode 100644 index 000000000..82328c8a5 --- /dev/null +++ b/app/models/exercice.rb @@ -0,0 +1,3 @@ +class Exercice < ActiveRecord::Base + belongs_to :etablissement +end diff --git a/app/views/dossiers/_infos_entreprise.html.haml b/app/views/dossiers/_infos_entreprise.html.haml index c60f9477d..fbf729ae9 100644 --- a/app/views/dossiers/_infos_entreprise.html.haml +++ b/app/views/dossiers/_infos_entreprise.html.haml @@ -5,24 +5,34 @@ .row#infos_entreprise .col-lg-6.col-md-6 %dl.dl-horizontal - %dt Siret : %dd.text-success= @etablissement.siret + - if @etablissement.siret != @entreprise.siret_siege_social + %dt SIRET siège social : + %dd= @entreprise.siret_siege_social + %dt Forme juridique : %dd= @entreprise.forme_juridique - %dt libelle naf : + %dt Libellé naf : %dd= @etablissement.libelle_naf + %dt Code naf : + %dd= @etablissement.naf + %dt Date de création : %dd= Time.at(@entreprise.date_creation).strftime "%d-%m-%Y" - %dt Effectife entreprise : + %dt Effectif entreprise : %dd= @entreprise.effectif - %dt Capital social : - %dd= @entreprise.pretty_capital_social + %dt Code effectif : + %dd= @entreprise.code_effectif_entreprise + + %dt Numéro TVA intracommunautaire : + %dd= @entreprise.numero_tva_intracommunautaire + .col-lg-6.col-md-6 %dl.dl-horizontal @@ -33,3 +43,14 @@ = line %br + %dt Capital social : + %dd= @entreprise.pretty_capital_social + + %dt Exercices : + %dd + %address + - @etablissement.exercices.each_with_index do |exercice, index| + %strong + = "#{exercice.dateFinExercice.year} : " + = number_to_currency(exercice.ca) + %br diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 3ade15838..de6f61940 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -20,6 +20,14 @@ # available at http://guides.rubyonrails.org/i18n.html. fr: + number: + currency: + format: + unit: '€' + delimiter: ' ' + separator: ',' + precision: 2 + format: '%n %u' devise: confirmations: confirmed: 'Votre compte a été confirmé avec succès.' diff --git a/db/migrate/20151113171605_create_exercice_table.rb b/db/migrate/20151113171605_create_exercice_table.rb new file mode 100644 index 000000000..d5d5c34c7 --- /dev/null +++ b/db/migrate/20151113171605_create_exercice_table.rb @@ -0,0 +1,11 @@ +class CreateExerciceTable < ActiveRecord::Migration + def change + create_table :exercices do |t| + t.string :ca + t.datetime :dateFinExercice + t.integer :date_fin_exercice_timestamp + end + + add_reference :exercices, :etablissement, references: :etablissements + end +end diff --git a/db/schema.rb b/db/schema.rb index 8b1c8c784..24d06b735 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20151110091451) do +ActiveRecord::Schema.define(version: 20151113171605) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -66,6 +66,7 @@ ActiveRecord::Schema.define(version: 20151110091451) do t.datetime "updated_at", default: '2015-09-22 09:25:29' t.string "state" t.integer "user_id" + t.text "json_latlngs" end add_index "dossiers", ["procedure_id"], name: "index_dossiers_on_procedure_id", using: :btree @@ -104,6 +105,13 @@ ActiveRecord::Schema.define(version: 20151110091451) do t.integer "entreprise_id" end + create_table "exercices", force: :cascade do |t| + t.string "ca" + t.datetime "dateFinExercice" + t.integer "date_fin_exercice_timestamp" + t.integer "etablissement_id" + end + create_table "gestionnaires", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false diff --git a/lib/siade/api.rb b/lib/siade/api.rb index d96470747..2bb92c15a 100644 --- a/lib/siade/api.rb +++ b/lib/siade/api.rb @@ -16,13 +16,20 @@ class SIADE::API call(base_url + endpoint) end - def self.call(url) + def self.exercices(siret) + endpoint = "/api/v1/etablissements/exercices/#{siret}" + call(base_url + endpoint) + end + + def self.call(url, params = {}) + params.merge!(token: SIADETOKEN) + verify_ssl_mode = OpenSSL::SSL::VERIFY_NONE RestClient::Resource.new( url, verify_ssl: verify_ssl_mode - ).get(params: { token: SIADETOKEN }) + ).get(params: params) end def self.base_url diff --git a/lib/siade/exercices_adapter.rb b/lib/siade/exercices_adapter.rb new file mode 100644 index 000000000..695e3157f --- /dev/null +++ b/lib/siade/exercices_adapter.rb @@ -0,0 +1,32 @@ +class SIADE::ExercicesAdapter + def initialize(siret) + @siret = siret + end + + def data_source + @data_source ||= JSON.parse(SIADE::API.exercices(@siret), symbolize_names: true) + rescue + @data_source = nil + end + + def to_params + params = {} + + data_source[:exercices].each_with_index do |values, i| + params[i] = {} + + values.each do |index, value| + params[i][index] = value if attr_to_fetch.include?(index) + end + end + params + rescue + nil + end + + def attr_to_fetch + [:ca, + :dateFinExercice, + :date_fin_exercice_timestamp] + end +end \ No newline at end of file diff --git a/spec/controllers/users/dossiers_controller_spec.rb b/spec/controllers/users/dossiers_controller_spec.rb index 87b2f7d75..7259aa68d 100644 --- a/spec/controllers/users/dossiers_controller_spec.rb +++ b/spec/controllers/users/dossiers_controller_spec.rb @@ -53,6 +53,9 @@ describe Users::DossiersController, type: :controller do stub_request(:get, "https://api-dev.apientreprise.fr/api/v1/entreprises/#{siren}?token=#{SIADETOKEN}") .to_return(status: 200, body: File.read('spec/support/files/entreprise.json')) + + stub_request(:get, "https://api-dev.apientreprise.fr/api/v1/etablissements/exercices/#{siret}?token=#{SIADETOKEN}") + .to_return(status: 200, body: File.read('spec/support/files/exercices.json')) end describe 'professionnel fills form' do @@ -98,6 +101,10 @@ describe Users::DossiersController, type: :controller do expect(Etablissement.last.entreprise).to eq(Entreprise.last) end + it 'creates exercices for dossier' do + expect { subject }.to change { Exercice.count }.by(3) + end + it 'links procedure to dossier' do subject expect(Dossier.last.procedure).to eq(Procedure.last) diff --git a/spec/decorators/entreprise_decorator_spec.rb b/spec/decorators/entreprise_decorator_spec.rb index 0b9c08a4a..1d918f8fc 100644 --- a/spec/decorators/entreprise_decorator_spec.rb +++ b/spec/decorators/entreprise_decorator_spec.rb @@ -7,11 +7,11 @@ describe EntrepriseDecorator do let(:prenom) { 'mon prenom' } let(:entreprise_params) do { - capital_social: 123_000, - code_effectif_entreprise: code_effectif, - raison_sociale: raison_sociale, - nom: nom, - prenom: prenom + capital_social: 123_000, + code_effectif_entreprise: code_effectif, + raison_sociale: raison_sociale, + nom: nom, + prenom: prenom } end @@ -47,7 +47,7 @@ describe EntrepriseDecorator do describe '#pretty_capital_social' do it 'pretty display capital_social' do - expect(subject.pretty_capital_social).to eq('123 000.00 €') + expect(subject.pretty_capital_social).to eq('123 000,00 €') end end diff --git a/spec/features/users/complete_demande_spec.rb b/spec/features/users/complete_demande_spec.rb index 194f14c04..941afbbfa 100644 --- a/spec/features/users/complete_demande_spec.rb +++ b/spec/features/users/complete_demande_spec.rb @@ -28,9 +28,12 @@ feature 'user path for dossier creation' do context 'sets siret' do before do stub_request(:get, "https://api-dev.apientreprise.fr/api/v1/etablissements/#{siret}?token=#{SIADETOKEN}") - .to_return(body: File.read('spec/support/files/etablissement.json', status: 200)) - stub_request(:get, "https://api-dev.apientreprise.fr/api/v1/entreprises/#{siren}?token=#{SIADETOKEN}") - .to_return(status: 200, body: File.read('spec/support/files/entreprise.json')) + .to_return(body: File.read('spec/support/files/etablissement.json', status: 200)) + stub_request(:get, "https://api-dev.apientreprise.fr/api/v1/entreprises/#{siren}?token=#{SIADETOKEN}") + .to_return(status: 200, body: File.read('spec/support/files/entreprise.json')) + + stub_request(:get, "https://api-dev.apientreprise.fr/api/v1/etablissements/exercices/#{siret}?token=#{SIADETOKEN}") + .to_return(status: 200, body: File.read('spec/support/files/exercices.json')) page.find_by_id('siret').set siret page.click_on 'Commencer' end diff --git a/spec/features/users/start_demande_spec.rb b/spec/features/users/start_demande_spec.rb index 62c4a6b88..f8543345f 100644 --- a/spec/features/users/start_demande_spec.rb +++ b/spec/features/users/start_demande_spec.rb @@ -26,9 +26,11 @@ feature 'user arrive on siret page' do context 'when enter a siret' do before do stub_request(:get, "https://api-dev.apientreprise.fr/api/v1/etablissements/#{siret}?token=#{SIADETOKEN}") - .to_return(status: 200, body: File.read('spec/support/files/etablissement.json')) + .to_return(status: 200, body: File.read('spec/support/files/etablissement.json')) stub_request(:get, "https://api-dev.apientreprise.fr/api/v1/entreprises/#{siren}?token=#{SIADETOKEN}") - .to_return(status: 200, body: File.read('spec/support/files/entreprise.json')) + .to_return(status: 200, body: File.read('spec/support/files/entreprise.json')) + stub_request(:get, "https://api-dev.apientreprise.fr/api/v1/etablissements/exercices/#{siret}?token=#{SIADETOKEN}") + .to_return(status: 200, body: File.read('spec/support/files/exercices.json')) page.find_by_id('siret').set siret page.click_on 'Commencer' end diff --git a/spec/lib/siade/api_spec.rb b/spec/lib/siade/api_spec.rb index 2e4f0a791..052117230 100644 --- a/spec/lib/siade/api_spec.rb +++ b/spec/lib/siade/api_spec.rb @@ -5,7 +5,7 @@ describe SIADE::API do subject { described_class.entreprise(siren) } before do stub_request(:get, "https://api-dev.apientreprise.fr/api/v1/entreprises/#{siren}?token=#{SIADETOKEN}") - .to_return(status: status, body: body) + .to_return(status: status, body: body) end context 'when siren does not exist' do let(:siren) { '111111111' } @@ -31,7 +31,7 @@ describe SIADE::API do subject { described_class.etablissement(siret) } before do stub_request(:get, "https://api-dev.apientreprise.fr/api/v1/etablissements/#{siret}?token=#{SIADETOKEN}") - .to_return(status: status, body: body) + .to_return(status: status, body: body) end context 'when siret does not exist' do @@ -54,4 +54,35 @@ describe SIADE::API do end end end + + describe '.exercices' do + before do + stub_request(:get, /https:\/\/api-dev.apientreprise.fr\/api\/v1\/etablissements\/exercices\/.*token=/) + .to_return(status: status, body: body) + end + + context 'when siret does not exist' do + subject { described_class.exercices(siret) } + + let(:siret) { '11111111111111' } + let(:status) { 404 } + let(:body) { '' } + + it 'raises RestClient::ResourceNotFound' do + expect { subject }.to raise_error(RestClient::ResourceNotFound) + end + end + + context 'when siret exists' do + subject { described_class.exercices(siret) } + + let(:siret) { '41816609600051' } + let(:status) { 200 } + let(:body) { File.read('spec/support/files/exercices.json') } + + it 'raises RestClient::Unauthorized' do + expect(subject).to eq(body) + end + end + end end diff --git a/spec/lib/siade/exercices_adapter_spec.rb b/spec/lib/siade/exercices_adapter_spec.rb new file mode 100644 index 000000000..777a7c7d6 --- /dev/null +++ b/spec/lib/siade/exercices_adapter_spec.rb @@ -0,0 +1,33 @@ +require 'spec_helper' + +describe SIADE::ExercicesAdapter do + let(:siret) { '41816609600051' } + subject { described_class.new(siret).to_params } + + before do + stub_request(:get, /https:\/\/api-dev.apientreprise.fr\/api\/v1\/etablissements\/exercices\/.*token=/) + .to_return(body: File.read('spec/support/files/exercices.json', status: 200)) + end + + it '#to_params class est une Hash ?' do + expect(subject).to be_an_instance_of(Hash) + end + + it 'have 3 exercices' do + expect(subject.size).to eq(3) + end + + context 'Attributs Exercices' do + it 'L\'exercice contient bien un ca' do + expect(subject[0][:ca]).to eq('21009417') + 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") + end + + it 'L\'exercice contient bien une date_fin_exercice_timestamp' do + expect(subject[0][:date_fin_exercice_timestamp]).to eq(1388444400) + end + end +end \ No newline at end of file diff --git a/spec/models/etablissement_spec.rb b/spec/models/etablissement_spec.rb index 0f8f09e7f..376886794 100644 --- a/spec/models/etablissement_spec.rb +++ b/spec/models/etablissement_spec.rb @@ -19,5 +19,6 @@ describe Etablissement do describe 'associations' do it { is_expected.to belong_to(:dossier) } it { is_expected.to belong_to(:entreprise) } + it { is_expected.to have_many(:exercices) } end end diff --git a/spec/models/exercice_spec.rb b/spec/models/exercice_spec.rb new file mode 100644 index 000000000..cfd6dc798 --- /dev/null +++ b/spec/models/exercice_spec.rb @@ -0,0 +1,13 @@ +require 'spec_helper' + +describe Exercice do + describe 'database columns' do + it { is_expected.to have_db_column(:ca) } + it { is_expected.to have_db_column(:dateFinExercice) } + it { is_expected.to have_db_column(:date_fin_exercice_timestamp) } + end + + describe 'associations' do + it { is_expected.to belong_to(:etablissement) } + end +end diff --git a/spec/support/files/exercices.json b/spec/support/files/exercices.json new file mode 100644 index 000000000..51b5064d5 --- /dev/null +++ b/spec/support/files/exercices.json @@ -0,0 +1,19 @@ +{ + "exercices":[ + { + "ca":"21009417", + "dateFinExercice":"2013-12-31T00:00:00+01:00", + "date_fin_exercice_timestamp": 1388444400 + }, + { + "ca":"18968298", + "dateFinExercice":"2012-12-31T00:00:00+01:00", + "date_fin_exercice_timestamp": 1356908400 + }, + { + "ca":"17768838", + "dateFinExercice":"2011-12-31T00:00:00+01:00", + "date_fin_exercice_timestamp": 1325286000 + } + ] +} \ No newline at end of file