demarches-normaliennes/app/controllers/dossiers_controller.rb

82 lines
2.1 KiB
Ruby
Raw Normal View History

2015-08-10 11:05:06 +02:00
class DossiersController < ApplicationController
def show
@dossier = Dossier.find(params[:id])
@etablissement = @dossier.etablissement
2015-08-10 11:05:06 +02:00
@entreprise = @dossier.entreprise.decorate
rescue ActiveRecord::RecordNotFound
2015-08-17 16:27:21 +02:00
redirect_to url_for(controller: :start, action: :error_dossier)
2015-08-10 11:05:06 +02:00
end
def create
@dossier_id = params[:pro_dossier_id].strip
2015-08-17 16:27:21 +02:00
if dossier_id_is_present?
2015-08-10 11:05:06 +02:00
@dossier = Dossier.find(@dossier_id)
if @dossier.siret == params[:siret]
2015-08-17 16:27:21 +02:00
redirect_to url_for(controller: :recapitulatif, action: :show, dossier_id: @dossier_id)
2015-08-10 11:05:06 +02:00
else
2015-08-17 16:27:21 +02:00
redirect_to url_for(controller: :start, action: :error_dossier)
2015-08-10 11:05:06 +02:00
end
else
2015-08-17 16:27:21 +02:00
@etablissement = Etablissement.new(SIADE::EtablissementAdapter.new(siret).to_params)
@entreprise = Entreprise.new(SIADE::EntrepriseAdapter.new(siren).to_params)
2015-08-10 11:05:06 +02:00
@dossier = Dossier.create
@entreprise.dossier = @dossier
@entreprise.save
@etablissement.dossier = @dossier
@etablissement.entreprise = @entreprise
@etablissement.save
2015-08-17 16:27:21 +02:00
redirect_to url_for(controller: :dossiers, action: :show, id: @dossier.id)
2015-08-10 11:05:06 +02:00
end
rescue RestClient::ResourceNotFound
2015-08-17 16:27:21 +02:00
redirect_to url_for(controller: :start, action: :error_siret)
rescue ActiveRecord::RecordNotFound
2015-08-17 16:27:21 +02:00
redirect_to url_for(controller: :start, action: :error_dossier)
2015-08-10 11:05:06 +02:00
end
def update
2015-08-10 11:05:06 +02:00
@dossier = Dossier.find(params[:id])
2015-08-17 16:27:21 +02:00
if checked_autorisation_donnees?
@dossier.update_attributes(update_params)
2015-08-10 11:05:06 +02:00
redirect_to url_for({controller: :demandes, action: :show, dossier_id: @dossier.id})
else
@etablissement = @dossier.etablissement
2015-08-10 11:05:06 +02:00
@entreprise = @dossier.entreprise.decorate
2015-08-17 16:27:21 +02:00
flash.now.alert = 'Les conditions sont obligatoires.'
render 'show'
2015-08-10 11:05:06 +02:00
end
end
2015-08-11 15:22:07 +02:00
private
def update_params
params.require(:dossier).permit(:autorisation_donnees)
end
2015-08-11 15:22:07 +02:00
def dossier_id_is_present?
2015-08-17 16:27:21 +02:00
@dossier_id != ""
end
2015-08-11 15:22:07 +02:00
2015-08-17 16:27:21 +02:00
def checked_autorisation_donnees?
update_params[:autorisation_donnees] == '1'
2015-08-11 15:22:07 +02:00
end
def siret
params[:siret]
end
def siren
siret[0..8]
end
2015-08-10 11:05:06 +02:00
end