class VolumePeriodique < ApplicationRecord
  acts_as_sorting :fields => {
     :id => {:name => "Id", :reorder => true},
     :codemanaginn => {:name => "Numéro CCV", :reorder => true},
     #:codeindicateur => {:name => "Codeindicateur", :reorder => true},
     :datedebut => {:name => "Date", :reorder => true, :as => :date},
     #:datefin => {:name => "Datefin", :reorder => true},
     :valeur => {:name => "Valeur", :reorder => true, :as => :currency},
     #:datereception => {:name => "Datereception", :reorder => true},
     :commentaire => {:name => "Commentaire", :reorder => true},
     :codesociete => {:name => "Code société", :reorder => true},
     #:created_at => {:name => "Created_at", :reorder => true},
     #:updated_at => {:name => "Updated_at", :reorder => true},
     :actions => {:name => "Actions", :reorder => true}
  }
  
  
  validates :valeur, :presence => true
  validates :datedebut, :presence => true
  
  
  attr_accessor :super_admin_power
  
  def super_admin_power?
    if self.super_admin_power
      true
    else
      false
    end
  end
  
  before_validation do
    #self.codesociete = self.v_contact.codeproprietaire
    self.codeindicateur = "MGI000000001" if !self.codeindicateur?
    
    
    errors.add(:valeur, "Doit être positif") if (!self.id and valeur.to_f < 0.0 and !super_admin_power?)
  end

  after_save do
    self.v_contact.save
    self.societe.save
  end

  belongs_to :v_contact, :foreign_key => :codemanaginn, :primary_key => :codemanaginn, :class_name => "VContact"
  belongs_to :societe, :foreign_key => :codesociete, :primary_key => :socmanaginn, :class_name => "Societe"

end
