class Societe < ApplicationRecord

  acts_as_sorting :fields => {
     :id => {:name => "Id", :reorder => true},
     :enabled => {:name => "Actif ?", :reorder => true, :as => :boolean},
     :socmanaginn => {:name => "Code Net-Inn", :reorder => true},
     :nom => {:name => "Nom", :reorder => true},
     
     :bon_type => {:name => "Type de réduction", :reorder => true},
     :seuil => {:name => "Seuil de déclenchement (€)", :reorder => true},
     :bon_amount => {:name => "Montant du bon d'achat (€)", :reorder => true},
     :send_via => {:name => "Mode d'envoi bon d'achat", :reorder => true},
     :remise_percent => {:name => "Remise immédiate (%)", :reorder => true},
     
     
     
     :adresse1 => {:name => "Adresse", :reorder => true},
     :adresse2 => {:name => "Adresse (suite)", :reorder => true},
     :codepostal => {:name => "Code postal", :reorder => true},
     :ville => {:name => "Ville", :reorder => true},
     :pays => {:name => "Pays", :reorder => true},
     :numtel => {:name => "Tel", :reorder => true},
     #:numfax => {:name => "Fax", :reorder => true},
     #:mobile => {:name => "Mobile", :reorder => true},
     :mail => {:name => "Email", :reorder => true},
     #:mailcc => {:name => "Email (cc)", :reorder => true},
     #:mailbcc => {:name => "Email (bcc)", :reorder => true},
     #:siteweb => {:name => "Site web", :reorder => true},
     #:statut => {:name => "Forme juridique", :reorder => true},
     #:rc => {:name => "RC", :reorder => true},
     #:naf => {:name => "NAF", :reorder => true},
     :soc_type => {:name => "Type de société", :reorder => true},
     :enseigne => {:name => "Enseigne", :reorder => true},
     #:datecreation => {:name => "Datecreation", :reorder => true},
     #:activite1 => {:name => "Activité 1", :reorder => true},
     #:activite2 => {:name => "Activité 2", :reorder => true},
     #:activite3 => {:name => "Activité 3", :reorder => true},
     #:activite4 => {:name => "Activité 4", :reorder => true},
     #:activite5 => {:name => "Activité 5", :reorder => true},
     :login => {:name => "Login", :reorder => true},
     :password => {:name => "Password", :reorder => true},
     #:photo => {:name => "Photo", :reorder => true},
     #:typeuser => {:name => "Typeuser", :reorder => true},
     #:codeproprietaire => {:name => "Codeproprietaire", :reorder => true},
     #:typepartage => {:name => "Typepartage", :reorder => true},
     #:libre1 => {:name => "Libre 1", :reorder => true},
     #:libre2 => {:name => "Libre 2", :reorder => true},
     #:libre3 => {:name => "Libre 3", :reorder => true},
     #:libre4 => {:name => "Libre 4", :reorder => true},
     :libre5 => {:name => "Libre 5", :reorder => true},
     #:accesbloque => {:name => "Accesbloque", :reorder => true},
     #:corbeille => {:name => "Corbeille", :reorder => true},
     #:codetpe => {:name => "Codetpe", :reorder => true},
     #:codecreateur => {:name => "Codecreateur", :reorder => true},
     #:partagedefaut => {:name => "Partagedefaut", :reorder => true},
     #:npai => {:name => "Npai", :reorder => true},
     #:npaimail => {:name => "Npaimail", :reorder => true},
     #:npaisms => {:name => "Npaisms", :reorder => true},
     #:ac_rate => {:name => "Ac_rate", :reorder => true},
     #:cc_rate => {:name => "Cc_rate", :reorder => true},
     :cc_cagnotte => {:name => "Cumul", :reorder => true, :as => :currency},
     #:cc_cagnotte => {:name => "Cc_cagnotte", :reorder => true},
     :cc_total => {:name => "CA total", :reorder => true, :as => :currency},
     #:cc_cumul => {:name => "Cc_cumul", :reorder => true},
     
     :last_volume_at => {:name => "Dernier mouvement", :reorder => true},
     #:created_at => {:name => "Created_at", :reorder => true},
     #:updated_at => {:name => "Updated_at", :reorder => true},
     :nbr_cartes => {:name => "Nombre de cartes"},
     
     :actions => {:name => "Actions", :reorder => true}
  }
  
  acts_as_caching :fields => [:total, :cagnotte, :remise]
  
  def ca_total
    self.cartes.sum(:cc_total)
  end
  
  def ca_cagnotte
    self.cartes.sum(:cc_cagnotte)
  end
  
  def ca_remise
    self.cartes.sum(:cc_remise)
  end
  
  def tr_nbr_cartes
    self.cartes.count
  end
  
  def v_contacts 
    self.cartes
    
  end
  
  def cartes
    VContact.where(:codeproprietaire => self.socmanaginn) #.where("codemanaginn LIKE 'CCV%'")
  end
  
  def cartes_ids
    cartes.select("codemanaginn").map{|a| a.codemanaginn}
    
  end
  
  def volumes
    VolumePeriodique.where(:codemanaginn => self.cartes_ids, :codeindicateur => "MGI000000001").order("datereception DESC")
    
  end
  
  before_validation do
    self.ac_total = nil
    self.ac_cagnotte = nil
    self.ac_remise = nil
    
    if self.bon_type == "Bon d'achat"
      if self.bon_amount.to_f != 0.0 and self.seuil.to_f != 0.0
        self.remise_percent = (self.bon_amount.to_f / seuil.to_f)*100.0
      else
        self.remise_percent = nil
      end
    else
      self.bon_amount = nil
      self.seuil = nil
      self.send_via = nil
      
    end
  end
  
  def self.init_logins
    Societe.where(:enabled => true).find_each do |s|
      a = Admin.create(:name => "", :firstname => "", :username => s.login, :password => s.password, :password_confirmation => s.password, :email => s.mail)
      a.admin_roles << AdminRole.where(:name => "Centre").first
      a.societe_id = s.id
      a.save
      puts a.errors.messages
    end
      
  end
  
  def self.update_all_caches
    Societe.all.each do |s|
      s.save
    end
  end
  
end
