class MOdrRepRib < ApplicationRecord
  belongs_to :admin
  
  belongs_to :p_customer

  belongs_to :m_odr_rep
  
  has_one :m_odr, :through => :m_odr_rep
  
  #validates :bic, :presence => true, length: { in: 8..11 }
  
  validates :iban, :presence => true
  
  validates_with IbanValidator
  
  has_many :mail_hists, :as => :element


  acts_as_sorting :fields => {
    :id => {:name => "Id", :reorder => true},
    :p_customer => {:name => "Utilisateur"},
    :iban => {:name => "IBAN", :reorder => true},
    :bic => {:name => "BIC", :reorder => true},
    :admin_ok => {:name => "Décision", :reorder => true},
    :reject_reason => {:name => "Raison du rejet", :reorder => true},
    :reject_reason_description => {:name => "Raison du rejet : commentaire", :reorder => true},
    :actions => {:name => "Actions", :reorder => true},

  }
  
  def update_prime_status
    if self.admin_ok and self.p_customer
      
      self.p_customer.m_odr_primes.where(:state => "Manque RIB").all.each do |m_odr_prime|
        m_odr_prime.state = "Validé"
        m_odr_prime.save
      end
    end
  end
  
end
