class SProject < ApplicationRecord
  acts_as_tree
  #validates :name, :presence => true
  belongs_to :p_commercial
  before_validation do
    if self.id
      errors.add(:parent_id, 'attention le projet parent à pour projet parente celui-ci = boucle') if self.parent_id and self.id == SProject.find(self.parent_id).parent_id
    end
  end
  
  has_many :s_project_modules
  has_many :s_modules, :through => :s_project_modules  
  
  has_many :price_documents
  
  belongs_to :p_customer
  
  acts_as_sorting :fields => {

    :p_number => {:name => "Code", :reorder => true},
    
    :name => {:name => "Nom", :reorder => true},
    :enabled => {:name => "Actif", :reorder => false, :format => :boolean},
    :parent_id => {:name => "Projet parent", :reorder => true},
    :p_customer => {:name => "Client", :reorder => false},
    :description => {:name => "Description", :reorder => true},
    :created_at => {:name => "Date de création", :reorder => true, :format => :date},
    :nav_order_no_customer => {:name => "N° Commande client", :reorder => true},
    #:nav_main_project_no => {:name => "", :reorder => true},
    :nav_project_status => {:name => "Etat", :reorder => true},
    :nav_project_type => {:name => "Type de projet", :reorder => true},
    :nav_estimate_no => {:name => "N° Devis", :reorder => true},
    :nav_site_production => {:name => "Site prod.", :reorder => true},
    :nav_starting_date => {:name => "Démarrage", :reorder => true},
    :nav_ending_date => {:name => "Fin", :reorder => true},
    :nav_last_modified_date => {:name => "Dernière modif", :reorder => true},
    :nav_technical_finished_date => {:name => "Fin technique", :reorder => true},
    :nav_commission_principal_date => {:name => "Commission principal date", :reorder => true},
    :nav_last_invoice_date => {:name => "Dernière facture", :reorder => true},
    :nav_livraison_client_date => {:name => "Date livraison", :reorder => true},

    
    
    :actions => {:name => "Actions", :reorder => false}
  }
  
  def member_label
    "#{self.p_number} - #{self.name}"
  end
  
  
  def state_to_text
    r = []
    r[0] = "Calcul"
    r[1] = "Préparation"
    r[2] = "Exécution"
    r[3] = "Finalisé technique"
    r[4] = "Finalisé administratif"
    r[5] = "Clôture comptable"
    r[6] = "Archiver"
    r[7] = "Archivé"
   
    
    return r
  end
  
  
  
end
