2018-04-10 12:24:49 +02:00
|
|
|
class ApiEntrepriseService
|
2019-04-30 16:36:40 +02:00
|
|
|
# Retrieve all informations we can get about a SIRET.
|
|
|
|
#
|
|
|
|
# Returns nil if the SIRET is unknown; and nested params
|
|
|
|
# suitable for being saved into a Etablissement object otherwise.
|
|
|
|
#
|
2020-03-05 13:50:38 +01:00
|
|
|
# Raises a ApiEntreprise::API::RequestFailed exception on transcient errors
|
2019-04-30 16:36:40 +02:00
|
|
|
# (timeout, 5XX HTTP error code, etc.)
|
2018-03-19 15:40:39 +01:00
|
|
|
def self.get_etablissement_params_for_siret(siret, procedure_id)
|
2018-03-19 14:44:47 +01:00
|
|
|
etablissement_params = ApiEntreprise::EtablissementAdapter.new(siret, procedure_id).to_params
|
2018-03-29 12:15:33 +02:00
|
|
|
entreprise_params = ApiEntreprise::EntrepriseAdapter.new(siret, procedure_id).to_params
|
2018-02-22 16:53:29 +01:00
|
|
|
|
2018-03-19 14:48:15 +01:00
|
|
|
if etablissement_params.present? && entreprise_params.present?
|
2019-05-03 17:24:42 +02:00
|
|
|
begin
|
|
|
|
association_params = ApiEntreprise::RNAAdapter.new(siret, procedure_id).to_params
|
|
|
|
etablissement_params.merge!(association_params)
|
2020-03-05 13:50:38 +01:00
|
|
|
rescue ApiEntreprise::API::RequestFailed
|
2019-05-03 17:24:42 +02:00
|
|
|
end
|
2018-02-22 16:53:29 +01:00
|
|
|
|
2019-05-03 17:24:42 +02:00
|
|
|
begin
|
|
|
|
exercices_params = ApiEntreprise::ExercicesAdapter.new(siret, procedure_id).to_params
|
|
|
|
etablissement_params.merge!(exercices_params)
|
2020-03-05 13:50:38 +01:00
|
|
|
rescue ApiEntreprise::API::RequestFailed
|
2019-05-03 17:24:42 +02:00
|
|
|
end
|
|
|
|
|
2020-04-07 19:29:14 +02:00
|
|
|
begin
|
|
|
|
effectifs_params = ApiEntreprise::EffectifsAdapter.new(entreprise_params[:entreprise_siren], procedure_id, *get_current_valid_month_for_effectif).to_params
|
|
|
|
etablissement_params.merge!(effectifs_params)
|
|
|
|
rescue ApiEntreprise::API::RequestFailed
|
|
|
|
end
|
|
|
|
|
2020-04-22 17:53:45 +02:00
|
|
|
begin
|
|
|
|
effectifs_annuels_params = ApiEntreprise::EffectifsAnnuelsAdapter.new(entreprise_params[:entreprise_siren], procedure_id).to_params
|
|
|
|
etablissement_params.merge!(effectifs_annuels_params)
|
|
|
|
rescue ApiEntreprise::API::RequestFailed
|
|
|
|
end
|
|
|
|
|
2019-05-03 17:24:42 +02:00
|
|
|
etablissement_params.merge(entreprise_params)
|
2018-02-22 16:53:29 +01:00
|
|
|
end
|
|
|
|
end
|
2020-04-07 19:29:14 +02:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def self.get_current_valid_month_for_effectif
|
|
|
|
today = Date.today
|
|
|
|
date_update = Date.new(today.year, today.month, 15)
|
|
|
|
|
|
|
|
if today >= date_update
|
|
|
|
[today.strftime("%Y"), today.strftime("%m")]
|
|
|
|
else
|
|
|
|
date = today - 1.month
|
|
|
|
[date.strftime("%Y"), date.strftime("%m")]
|
|
|
|
end
|
|
|
|
end
|
2018-02-22 16:53:29 +01:00
|
|
|
end
|