diff --git a/app/helpers/etablissement_helper.rb b/app/helpers/etablissement_helper.rb index cd705bd6f..6c95294d6 100644 --- a/app/helpers/etablissement_helper.rb +++ b/app/helpers/etablissement_helper.rb @@ -1,13 +1,29 @@ module EtablissementHelper def value_for_bilan_key(bilan, key) - if bilan["data"] - return "NA" if key == "resultat_exercice" + if bilan_v3?(bilan) + return extract_resultat_exercice(bilan['data']) if key == "resultat_exercice" bilan["data"][key].presence || bilan["data"]["valeurs_calculees"][0][key].present? ? bilan["data"]["valeurs_calculees"][0][key]["valeur"] : nil else bilan[key] end end + # trouver la declaration 2051, et prendre la premiere valeur du bilan identifié par le code code_nref: 300476 + # autrement connu comme le resultat d'un exercice dans un bilan comptable "funky magic accountant lingo" + def extract_resultat_exercice(bilan) + declaration_2051 = bilan.dig('declarations').find { _1["numero_imprime"] == "2051" } + return nil if declaration_2051.nil? + + total_general_data = declaration_2051.dig("donnees").find { _1["code_nref"] == "300476" } + return nil if total_general_data.nil? + + total_general_data.dig("valeurs", 0) + end + + def bilan_v3?(bilan) + bilan&.key?("data") + end + def pretty_siret(siret) "#{siret[0..2]} #{siret[3..5]} #{siret[6..8]} #{siret[9..]}" end diff --git a/spec/fixtures/files/api_entreprise/bilans_entreprise_bdf_v2.json b/spec/fixtures/files/api_entreprise/bilans_entreprise_bdf_v2.json index 1f2350363..5416840c5 100644 --- a/spec/fixtures/files/api_entreprise/bilans_entreprise_bdf_v2.json +++ b/spec/fixtures/files/api_entreprise/bilans_entreprise_bdf_v2.json @@ -46,6 +46,6 @@ "evolution_total_dettes_stables": "", "fonds_roulement_net_global": "2464585", "ratio_fonds_roulement_net_global_sur_besoin_en_fonds_de_roulement": "-" - }, "bilan 2", "bilan 3" + } ] } diff --git a/spec/fixtures/files/api_entreprise/bilans_entreprise_bdf_with_bilans.json b/spec/fixtures/files/api_entreprise/bilans_entreprise_bdf_with_bilans.json new file mode 100644 index 000000000..95ddd8a82 --- /dev/null +++ b/spec/fixtures/files/api_entreprise/bilans_entreprise_bdf_with_bilans.json @@ -0,0 +1,628 @@ +{ + "data":[ + { + "data":{ + "annee":"2022", + "date_arrete_exercice":"2022-08", + "declarations":[ + { + "numero_imprime":"2051", + "donnees":[ + { + "code_nref":"300476", + "valeurs":[ + "97065000" + ], + "evolution":55.85, + "code_absolu":"2000536", + "intitule":"Total général bilan passif n , exercice n total i + total ii + total iii + total iv + total v", + "code_EDI":"EE:C516:5004:1", + "code":"EE", + "code_type_donnee":"MOA" + }, + { + "code_nref":"300438", + "valeurs":[ + "35042000" + ], + "evolution":50.71, + "code_absolu":"2000497", + "intitule":"Total (i) exercice n , total des capitaux propres n", + "code_EDI":"DL:C516:5004:1", + "code":"DL", + "code_type_donnee":"MOA" + }, + { + "code_nref":"300414", + "valeurs":[ + "510000" + ], + "evolution":0.0, + "code_absolu":"2000472", + "intitule":"Capital social ou individuel n", + "code_EDI":"DA:C516:5004:1", + "code":"DA", + "code_type_donnee":"MOA" + }, + { + "code_nref":"302359", + "valeurs":[ + "0" + ], + "evolution":null, + "code_absolu":"2002767", + "intitule":"Total (iii) exercice n , total des provisions pour risques et charges n", + "code_EDI":"DR:C516:5004:1", + "code":"DR", + "code_type_donnee":"MOA" + }, + { + "code_nref":"300451", + "valeurs":[ + "0" + ], + "evolution":null, + "code_absolu":"2000511", + "intitule":"Emprunts obligataires convertibles n", + "code_EDI":"DS:C516:5004:1", + "code":"DS", + "code_type_donnee":"MOA" + }, + { + "code_nref":"300453", + "valeurs":[ + "0" + ], + "evolution":null, + "code_absolu":"2000513", + "intitule":"Autres emprunts obligataires n", + "code_EDI":"DT:C516:5004:1", + "code":"DT", + "code_type_donnee":"MOA" + }, + { + "code_nref":"300455", + "valeurs":[ + "0" + ], + "evolution":null, + "code_absolu":"2000515", + "intitule":"Emprunts et dettes auprès des établissements de crédit n", + "code_EDI":"DU:C516:5004:1", + "code":"DU", + "code_type_donnee":"MOA" + }, + { + "code_nref":"300458", + "valeurs":[ + "0" + ], + "evolution":null, + "code_absolu":"2000518", + "intitule":"Emprunts et dettes financières divers n", + "code_EDI":"DV:C516:5004:1", + "code":"DV", + "code_type_donnee":"MOA" + } + ] + }, + { + "numero_imprime":"2052", + "donnees":[ + { + "code_nref":"300506", + "valeurs":[ + "152238000" + ], + "evolution":18.7, + "code_absolu":"2000566", + "intitule":"Produits d'exploitation chiffre d'affaires net total", + "code_EDI":"FL:C516:5004:1", + "code":"FL", + "code_type_donnee":"MOA" + } + ] + }, + { + "numero_imprime":"2053", + "donnees":[ + { + "code_nref":"300606", + "valeurs":[ + "11790000" + ], + "evolution":26.56, + "code_absolu":"2000666", + "intitule":"5 - bénéfice ou perte (total des produits - total des charges)", + "code_EDI":"HN:C516:5004:1", + "code":"HN", + "code_type_donnee":"MOA" + } + ] + }, + { + "numero_imprime":"2057", + "donnees":[ + { + "code_nref":"301195", + "valeurs":[ + "0" + ], + "evolution":null, + "code_absolu":"2001265", + "intitule":"Dettes groupe, communauté d'exploitation et associés, montant brut", + "code_EDI":"VI:C516:5004:1", + "code":"VI", + "code_type_donnee":"MOA" + } + ] + } + ], + "valeurs_calculees":[ + { + "disponibilites":{ + "valeur":"1468000", + "evolution":376.62 + }, + "total_dettes_stables":{ + "valeur":"0", + "evolution":null + }, + "valeur_ajoutee_bdf":{ + "valeur":"102165000", + "evolution":15.82 + }, + "besoin_en_fonds_de_roulement":{ + "valeur":"-9703000", + "evolution":-151.08 + }, + "excedent_brut_exploitation":{ + "valeur":"18259000", + "evolution":22.87 + }, + "capacite_autofinancement":{ + "valeur":"10224000", + "evolution":-4.05 + }, + "fonds_roulement_net_global":{ + "valeur":"-8236000", + "evolution":-142.67 + }, + "ratio_fonds_roulement_net_global_sur_besoin_en_fonds_de_roulement":{ + "valeur":0.0, + "evolution":null + }, + "dettes4_maturite_a_un_an_au_plus":{ + "valeur":"0", + "evolution":null + } + } + ] + }, + "links":{ + + }, + "meta":{ + + } + }, + { + "data":{ + "annee":"2021", + "date_arrete_exercice":"2021-08", + "declarations":[ + { + "numero_imprime":"2051", + "donnees":[ + { + "code_nref":"300476", + "valeurs":[ + "62283000" + ], + "evolution":25.39, + "code_absolu":"2000536", + "intitule":"Total général bilan passif n , exercice n total i + total ii + total iii + total iv + total v", + "code_EDI":"EE:C516:5004:1", + "code":"EE", + "code_type_donnee":"MOA" + }, + { + "code_nref":"300438", + "valeurs":[ + "23252000" + ], + "evolution":26.94, + "code_absolu":"2000497", + "intitule":"Total (i) exercice n , total des capitaux propres n", + "code_EDI":"DL:C516:5004:1", + "code":"DL", + "code_type_donnee":"MOA" + }, + { + "code_nref":"300414", + "valeurs":[ + "510000" + ], + "evolution":0.0, + "code_absolu":"2000472", + "intitule":"Capital social ou individuel n", + "code_EDI":"DA:C516:5004:1", + "code":"DA", + "code_type_donnee":"MOA" + }, + { + "code_nref":"302359", + "valeurs":[ + "1165000" + ], + "evolution":30.9, + "code_absolu":"2002767", + "intitule":"Total (iii) exercice n , total des provisions pour risques et charges n", + "code_EDI":"DR:C516:5004:1", + "code":"DR", + "code_type_donnee":"MOA" + }, + { + "code_nref":"300451", + "valeurs":[ + "0" + ], + "evolution":null, + "code_absolu":"2000511", + "intitule":"Emprunts obligataires convertibles n", + "code_EDI":"DS:C516:5004:1", + "code":"DS", + "code_type_donnee":"MOA" + }, + { + "code_nref":"300453", + "valeurs":[ + "0" + ], + "evolution":null, + "code_absolu":"2000513", + "intitule":"Autres emprunts obligataires n", + "code_EDI":"DT:C516:5004:1", + "code":"DT", + "code_type_donnee":"MOA" + }, + { + "code_nref":"300455", + "valeurs":[ + "0" + ], + "evolution":null, + "code_absolu":"2000515", + "intitule":"Emprunts et dettes auprès des établissements de crédit n", + "code_EDI":"DU:C516:5004:1", + "code":"DU", + "code_type_donnee":"MOA" + }, + { + "code_nref":"300458", + "valeurs":[ + "0" + ], + "evolution":null, + "code_absolu":"2000518", + "intitule":"Emprunts et dettes financières divers n", + "code_EDI":"DV:C516:5004:1", + "code":"DV", + "code_type_donnee":"MOA" + } + ] + }, + { + "numero_imprime":"2052", + "donnees":[ + { + "code_nref":"300506", + "valeurs":[ + "128252000" + ], + "evolution":24.09, + "code_absolu":"2000566", + "intitule":"Produits d'exploitation chiffre d'affaires net total", + "code_EDI":"FL:C516:5004:1", + "code":"FL", + "code_type_donnee":"MOA" + } + ] + }, + { + "numero_imprime":"2053", + "donnees":[ + { + "code_nref":"300606", + "valeurs":[ + "9316000" + ], + "evolution":167.32, + "code_absolu":"2000666", + "intitule":"5 - bénéfice ou perte (total des produits - total des charges)", + "code_EDI":"HN:C516:5004:1", + "code":"HN", + "code_type_donnee":"MOA" + } + ] + }, + { + "numero_imprime":"2057", + "donnees":[ + { + "code_nref":"301195", + "valeurs":[ + "0" + ], + "evolution":null, + "code_absolu":"2001265", + "intitule":"Dettes groupe, communauté d'exploitation et associés, montant brut", + "code_EDI":"VI:C516:5004:1", + "code":"VI", + "code_type_donnee":"MOA" + } + ] + } + ], + "valeurs_calculees":[ + { + "disponibilites":{ + "valeur":"308000", + "evolution":0.65 + }, + "total_dettes_stables":{ + "valeur":"0", + "evolution":null + }, + "valeur_ajoutee_bdf":{ + "valeur":"88210000", + "evolution":25.62 + }, + "besoin_en_fonds_de_roulement":{ + "valeur":"18995000", + "evolution":52.19 + }, + "excedent_brut_exploitation":{ + "valeur":"14860000", + "evolution":75.88 + }, + "capacite_autofinancement":{ + "valeur":"10656000", + "evolution":86.59 + }, + "fonds_roulement_net_global":{ + "valeur":"19303000", + "evolution":50.96 + }, + "ratio_fonds_roulement_net_global_sur_besoin_en_fonds_de_roulement":{ + "valeur":101.62, + "evolution":-0.81 + }, + "dettes4_maturite_a_un_an_au_plus":{ + "valeur":"0", + "evolution":null + } + } + ] + }, + "links":{ + + }, + "meta":{ + + } + }, + { + "data":{ + "annee":"2020", + "date_arrete_exercice":"2020-08", + "declarations":[ + { + "numero_imprime":"2051", + "donnees":[ + { + "code_nref":"300476", + "valeurs":[ + "49672000" + ], + "evolution":null, + "code_absolu":"2000536", + "intitule":"Total général bilan passif n , exercice n total i + total ii + total iii + total iv + total v", + "code_EDI":"EE:C516:5004:1", + "code":"EE", + "code_type_donnee":"MOA" + }, + { + "code_nref":"300438", + "valeurs":[ + "18318000" + ], + "evolution":null, + "code_absolu":"2000497", + "intitule":"Total (i) exercice n , total des capitaux propres n", + "code_EDI":"DL:C516:5004:1", + "code":"DL", + "code_type_donnee":"MOA" + }, + { + "code_nref":"300414", + "valeurs":[ + "510000" + ], + "evolution":null, + "code_absolu":"2000472", + "intitule":"Capital social ou individuel n", + "code_EDI":"DA:C516:5004:1", + "code":"DA", + "code_type_donnee":"MOA" + }, + { + "code_nref":"302359", + "valeurs":[ + "890000" + ], + "evolution":null, + "code_absolu":"2002767", + "intitule":"Total (iii) exercice n , total des provisions pour risques et charges n", + "code_EDI":"DR:C516:5004:1", + "code":"DR", + "code_type_donnee":"MOA" + }, + { + "code_nref":"300451", + "valeurs":[ + "0" + ], + "evolution":null, + "code_absolu":"2000511", + "intitule":"Emprunts obligataires convertibles n", + "code_EDI":"DS:C516:5004:1", + "code":"DS", + "code_type_donnee":"MOA" + }, + { + "code_nref":"300453", + "valeurs":[ + "0" + ], + "evolution":null, + "code_absolu":"2000513", + "intitule":"Autres emprunts obligataires n", + "code_EDI":"DT:C516:5004:1", + "code":"DT", + "code_type_donnee":"MOA" + }, + { + "code_nref":"300455", + "valeurs":[ + "0" + ], + "evolution":null, + "code_absolu":"2000515", + "intitule":"Emprunts et dettes auprès des établissements de crédit n", + "code_EDI":"DU:C516:5004:1", + "code":"DU", + "code_type_donnee":"MOA" + }, + { + "code_nref":"300458", + "valeurs":[ + "0" + ], + "evolution":null, + "code_absolu":"2000518", + "intitule":"Emprunts et dettes financières divers n", + "code_EDI":"DV:C516:5004:1", + "code":"DV", + "code_type_donnee":"MOA" + } + ] + }, + { + "numero_imprime":"2052", + "donnees":[ + { + "code_nref":"300506", + "valeurs":[ + "103357000" + ], + "evolution":null, + "code_absolu":"2000566", + "intitule":"Produits d'exploitation chiffre d'affaires net total", + "code_EDI":"FL:C516:5004:1", + "code":"FL", + "code_type_donnee":"MOA" + } + ] + }, + { + "numero_imprime":"2053", + "donnees":[ + { + "code_nref":"300606", + "valeurs":[ + "3485000" + ], + "evolution":null, + "code_absolu":"2000666", + "intitule":"5 - bénéfice ou perte (total des produits - total des charges)", + "code_EDI":"HN:C516:5004:1", + "code":"HN", + "code_type_donnee":"MOA" + } + ] + }, + { + "numero_imprime":"2057", + "donnees":[ + { + "code_nref":"301195", + "valeurs":[ + "0" + ], + "evolution":null, + "code_absolu":"2001265", + "intitule":"Dettes groupe, communauté d'exploitation et associés, montant brut", + "code_EDI":"VI:C516:5004:1", + "code":"VI", + "code_type_donnee":"MOA" + } + ] + } + ], + "valeurs_calculees":[ + { + "disponibilites":{ + "valeur":"306000", + "evolution":null + }, + "total_dettes_stables":{ + "valeur":"0", + "evolution":null + }, + "valeur_ajoutee_bdf":{ + "valeur":"70217000", + "evolution":null + }, + "besoin_en_fonds_de_roulement":{ + "valeur":"12481000", + "evolution":null + }, + "excedent_brut_exploitation":{ + "valeur":"8449000", + "evolution":null + }, + "capacite_autofinancement":{ + "valeur":"5711000", + "evolution":null + }, + "fonds_roulement_net_global":{ + "valeur":"12787000", + "evolution":null + }, + "ratio_fonds_roulement_net_global_sur_besoin_en_fonds_de_roulement":{ + "valeur":102.45, + "evolution":null + }, + "dettes4_maturite_a_un_an_au_plus":{ + "valeur":"0", + "evolution":null + } + } + ] + }, + "links":{ + + }, + "meta":{ + + } + } + ], + "meta":{ + "count":3 + }, + "links":{ + + } +} diff --git a/spec/helpers/etablissement_helper_spec.rb b/spec/helpers/etablissement_helper_spec.rb index d17c071ae..bb1248b95 100644 --- a/spec/helpers/etablissement_helper_spec.rb +++ b/spec/helpers/etablissement_helper_spec.rb @@ -24,6 +24,25 @@ RSpec.describe EtablissementHelper, type: :helper do it { is_expected.to eq("123 456 789 00001") } end + describe "#extract_resultat_exercice" do + let(:bilan) { bilans.first } + context 'having results' do + let(:bilans) { JSON.parse(File.read('spec/fixtures/files/api_entreprise/bilans_entreprise_bdf_with_bilans.json'))["data"] } + + it 'find value' do + expect(extract_resultat_exercice(bilan["data"])).to eq("97065000") + end + end + + context 'without results' do + let(:bilans) { JSON.parse(File.read('spec/fixtures/files/api_entreprise/bilans_entreprise_bdf.json'))["data"] } + + it 'does not crash' do + expect(extract_resultat_exercice(bilan["data"])).to eq(nil) + end + end + end + describe '#raison_sociale_or_name' do subject { raison_sociale_or_name(etablissement) } diff --git a/spec/lib/api_entreprise/api_spec.rb b/spec/lib/api_entreprise/api_spec.rb index 21c9e1a21..e74017b9b 100644 --- a/spec/lib/api_entreprise/api_spec.rb +++ b/spec/lib/api_entreprise/api_spec.rb @@ -239,7 +239,7 @@ describe APIEntreprise::API do before do allow_any_instance_of(APIEntrepriseToken).to receive(:roles).and_return(roles) allow_any_instance_of(APIEntrepriseToken).to receive(:expired?).and_return(false) - stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v2\/bilans_entreprises_bdf\/#{siren}/) + stub_request(:get, /https:\/\/entreprise.api.gouv.fr\/v3\/banque_de_france\/unites_legales\/#{siren}\/bilans/) .to_return(body: body, status: status) end diff --git a/spec/views/shared/dossiers/_identite_entreprise.html.haml_spec.rb b/spec/views/shared/dossiers/_identite_entreprise.html.haml_spec.rb index 2a3360c86..845bf5d94 100644 --- a/spec/views/shared/dossiers/_identite_entreprise.html.haml_spec.rb +++ b/spec/views/shared/dossiers/_identite_entreprise.html.haml_spec.rb @@ -3,7 +3,6 @@ describe 'shared/dossiers/identite_entreprise', type: :view do let(:profile) { 'usager' } context "there is an association" do - let(:etablissement) { create(:etablissement, :is_association) } context "date_publication is missing on rna" do