demarches-normaliennes/app/models/etablissement.rb

53 lines
1.3 KiB
Ruby
Raw Normal View History

2018-03-06 13:44:29 +01:00
class Etablissement < ApplicationRecord
2015-08-10 11:05:06 +02:00
belongs_to :dossier
belongs_to :entreprise, dependent: :destroy
2018-04-03 16:27:59 +02:00
has_one :champ, class_name: 'Champs::SiretChamp'
has_many :exercices, dependent: :destroy
2018-02-22 16:53:29 +01:00
accepts_nested_attributes_for :exercices
accepts_nested_attributes_for :entreprise, update_only: true
2018-02-22 16:53:29 +01:00
2018-04-03 17:53:14 +02:00
validates :siret, presence: true
validates :dossier_id, uniqueness: { allow_nil: true }
validate :validate_signature
def geo_adresse
[numero_voie, type_voie, nom_voie, complement_adresse, code_postal, localite].join(' ')
end
2017-06-22 14:58:29 +02:00
def inline_adresse
# 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
def titre
entreprise_raison_sociale || association_titre
end
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-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] }
}).delete_if { |k,v| v.blank? })
end
2015-08-10 11:05:06 +02:00
end