2018-03-06 13:44:29 +01:00
|
|
|
class Etablissement < ApplicationRecord
|
2015-08-10 11:05:06 +02:00
|
|
|
belongs_to :dossier
|
2015-11-16 11:23:29 +01:00
|
|
|
|
2018-04-03 16:27:59 +02:00
|
|
|
has_one :champ, class_name: 'Champs::SiretChamp'
|
2016-06-20 13:57:57 +02:00
|
|
|
has_many :exercices, dependent: :destroy
|
2016-01-06 15:48:06 +01:00
|
|
|
|
2018-02-22 16:53:29 +01:00
|
|
|
accepts_nested_attributes_for :exercices
|
|
|
|
|
2018-04-03 17:53:14 +02:00
|
|
|
validates :siret, presence: true
|
|
|
|
validates :dossier_id, uniqueness: { allow_nil: true }
|
|
|
|
|
|
|
|
validate :validate_signature
|
2016-10-05 14:28:10 +02:00
|
|
|
|
2018-07-25 19:34:06 +02:00
|
|
|
def search_terms
|
|
|
|
[
|
|
|
|
entreprise_siren,
|
|
|
|
entreprise_numero_tva_intracommunautaire,
|
|
|
|
entreprise_forme_juridique,
|
|
|
|
entreprise_forme_juridique_code,
|
|
|
|
entreprise_nom_commercial,
|
|
|
|
entreprise_raison_sociale,
|
|
|
|
entreprise_siret_siege_social,
|
|
|
|
entreprise_nom,
|
|
|
|
entreprise_prenom,
|
|
|
|
association_rna,
|
|
|
|
association_titre,
|
|
|
|
association_objet,
|
|
|
|
siret,
|
|
|
|
naf,
|
|
|
|
libelle_naf,
|
|
|
|
adresse,
|
|
|
|
code_postal,
|
|
|
|
localite,
|
|
|
|
code_insee_localite
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2018-04-23 11:57:38 +02:00
|
|
|
def siren
|
|
|
|
entreprise_siren
|
|
|
|
end
|
|
|
|
|
2016-01-06 15:48:06 +01:00
|
|
|
def geo_adresse
|
2016-01-19 17:19:38 +01:00
|
|
|
[numero_voie, type_voie, nom_voie, complement_adresse, code_postal, localite].join(' ')
|
2016-01-06 15:48:06 +01:00
|
|
|
end
|
2017-06-22 14:58:29 +02:00
|
|
|
|
|
|
|
def inline_adresse
|
2018-01-15 19:14:09 +01:00
|
|
|
# squeeze needed because of space in excess in the data
|
2017-06-22 14:58:29 +02:00
|
|
|
"#{numero_voie} #{type_voie} #{nom_voie}, #{complement_adresse}, #{code_postal} #{localite}".squeeze(' ')
|
|
|
|
end
|
2018-04-03 17:53:14 +02:00
|
|
|
|
2018-04-03 16:27:59 +02:00
|
|
|
def verify
|
|
|
|
SignatureService.verify(signature, message_for_signature)
|
|
|
|
end
|
|
|
|
|
|
|
|
def sign
|
|
|
|
SignatureService.sign(message_for_signature)
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_accessor :signature
|
|
|
|
|
2018-04-23 11:57:38 +02:00
|
|
|
def association?
|
|
|
|
association_rna.present?
|
2018-04-05 18:54:33 +02:00
|
|
|
end
|
|
|
|
|
2018-04-23 11:57:38 +02:00
|
|
|
def entreprise
|
|
|
|
Entreprise.new(
|
|
|
|
siren: entreprise_siren,
|
|
|
|
capital_social: entreprise_capital_social,
|
|
|
|
numero_tva_intracommunautaire: entreprise_numero_tva_intracommunautaire,
|
|
|
|
forme_juridique: entreprise_forme_juridique,
|
|
|
|
forme_juridique_code: entreprise_forme_juridique_code,
|
|
|
|
nom_commercial: entreprise_nom_commercial,
|
|
|
|
raison_sociale: entreprise_raison_sociale,
|
|
|
|
siret_siege_social: entreprise_siret_siege_social,
|
|
|
|
code_effectif_entreprise: entreprise_code_effectif_entreprise,
|
|
|
|
date_creation: entreprise_date_creation,
|
|
|
|
nom: entreprise_nom,
|
|
|
|
prenom: entreprise_prenom,
|
|
|
|
inline_adresse: inline_adresse
|
|
|
|
)
|
2018-04-05 18:54:33 +02:00
|
|
|
end
|
|
|
|
|
2018-04-03 17:53:14 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
def validate_signature
|
|
|
|
if champ && !verify
|
|
|
|
errors.add(:base, 'Numéro SIRET introuvable.')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-04-03 16:27:59 +02:00
|
|
|
def message_for_signature
|
|
|
|
JSON.pretty_generate(as_json(include: {
|
|
|
|
exercices: { only: [:ca, :date_fin_exercice, :date_fin_exercice_timestamp] }
|
2018-12-24 17:53:16 +01:00
|
|
|
}).delete_if { |_k, v| v.blank? })
|
2018-04-03 16:27:59 +02:00
|
|
|
end
|
2015-08-10 11:05:06 +02:00
|
|
|
end
|