2018-04-03 17:53:14 +02:00
|
|
|
class Champs::SiretController < ApplicationController
|
2018-09-06 19:23:27 +02:00
|
|
|
before_action :authenticate_logged_user!
|
|
|
|
|
|
|
|
def show
|
|
|
|
@position = params[:position]
|
|
|
|
extract_siret
|
|
|
|
find_etablisement
|
|
|
|
|
|
|
|
if @siret.empty?
|
2019-03-12 14:57:03 +01:00
|
|
|
@champ&.update!(value: '')
|
|
|
|
@etablissement&.destroy
|
2018-09-06 19:23:27 +02:00
|
|
|
elsif @siret.present? && @siret.length == 14
|
|
|
|
etablissement = find_etablisement_with_siret
|
|
|
|
if etablissement.present?
|
|
|
|
@etablissement = etablissement
|
2019-04-01 16:24:56 +02:00
|
|
|
|
|
|
|
if !@champ.nil?
|
|
|
|
@champ.update!(value: etablissement.siret, etablissement: etablissement)
|
2019-03-12 14:57:03 +01:00
|
|
|
end
|
2018-04-03 17:53:14 +02:00
|
|
|
else
|
2019-03-12 14:57:03 +01:00
|
|
|
@champ&.update!(value: '')
|
|
|
|
@etablissement&.destroy
|
2018-09-06 19:23:27 +02:00
|
|
|
@siret = :not_found
|
2018-04-03 17:53:14 +02:00
|
|
|
end
|
2018-09-06 19:23:27 +02:00
|
|
|
else
|
2019-03-12 14:57:03 +01:00
|
|
|
@champ&.update!(value: '')
|
|
|
|
@etablissement&.destroy
|
2018-09-06 19:23:27 +02:00
|
|
|
@siret = :invalid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def extract_siret
|
|
|
|
if params[:dossier].key?(:champs_attributes)
|
|
|
|
@siret = params[:dossier][:champs_attributes][@position][:value]
|
|
|
|
@attribute = "dossier[champs_attributes][#{@position}][etablissement_attributes]"
|
|
|
|
else
|
|
|
|
@siret = params[:dossier][:champs_private_attributes][@position][:value]
|
|
|
|
@attribute = "dossier[champs_private_attributes][#{@position}][etablissement_attributes]"
|
2018-04-03 17:53:14 +02:00
|
|
|
end
|
2018-09-06 19:23:27 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_etablisement
|
|
|
|
if params[:champ_id].present?
|
2019-03-12 14:57:03 +01:00
|
|
|
@champ = Champ.find_by(dossier_id: logged_user.dossiers, id: params[:champ_id])
|
|
|
|
@etablissement = @champ&.etablissement
|
2018-09-06 19:23:27 +02:00
|
|
|
end
|
2019-03-12 14:57:03 +01:00
|
|
|
@procedure_id = @champ&.dossier&.procedure_id || 'aperçu'
|
2018-09-06 19:23:27 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_etablisement_with_siret
|
|
|
|
etablissement_attributes = ApiEntrepriseService.get_etablissement_params_for_siret(@siret, @procedure_id)
|
|
|
|
if etablissement_attributes.present?
|
|
|
|
Etablissement.new(etablissement_attributes)
|
2018-04-03 17:53:14 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|