class PCustomerSheet < ActiveRecord::Base
  has_many :p_documents, :as => :element
  belongs_to :p_customer
  accepts_nested_attributes_for :p_customer
  
  belongs_to :p_customer_site
  
  has_many :p_sheet_lines
  accepts_nested_attributes_for :p_sheet_lines, allow_destroy: true
  
  validates :p_customer_id, :presence => true
  validates :particular_bill_id, :presence => true
  validates :particular_send_id, :presence => true
  
  belongs_to :particular_bill, :class_name => "Particular"#, :dependent => :destroy
  
  accepts_nested_attributes_for :particular_bill
  
  belongs_to :particular_send, :class_name => "Particular"#, :dependent => :destroy
  
  accepts_nested_attributes_for :particular_send
  
  
  before_save do

    if !self.imported
      self.a_total_cost_ok = self.p_sheet_lines.sum(:a_total_cost_ok) 
   
    
      totals =  PSheetLine.totals(self.p_sheet_lines)
    
      self.a_ok_total = totals[:ok_total]
    
      if (self.state == "livrée" or self.state == "facturée") and self.p_sheet_lines.where("p_customer_sheet_id is not null").where(:externe => false, :stock_done => false, :shiped => true).count == 0
        self.stock_done = true
      else
        self.stock_done = false
    
      end
    end
    
    true
    
  end
  
  def sheet_lines_shiped
    self.p_sheet_lines.oks
    
  end
  
  def sheet_lines_not_shiped
    self.p_sheet_lines.not_oks
    
  end
  
  def all_lines_shiped?
    if sheet_lines_not_shiped.count == 0
      true
    else
      false
    end
  end
  
  after_save do
    if self.state == "commande" or self.state == "livraison-en-cours"
      if self.all_lines_shiped?
        self.state = "livrée"
        self.save
      end
    end
  end
  
  before_validation do 
    if !self.p_customer or !self.particular_bill or !self.particular_bill.owner or self.particular_bill.owner != self.p_customer
      errors.add(:particular_bill_id, 'doit être une adresse du client') 
    end
    
    if !self.p_customer or !self.particular_send or !self.particular_send.owner or self.particular_send.owner != self.p_customer
      errors.add(:particular_send_id, 'doit être une adresse du client') 
    end
  end
  
  
  def state_html
    if self.state == "brouillon"
      '<span class="badge badge-pill badge-warning">Brouillon</span>'
    elsif self.state == "offre"
      '<span class="badge badge-warning">Offre</span>'
    elsif self.state == "commande"
      '<span class="badge badge-success">Commande</span>'
    elsif self.state == "livraison-en-cours"
      '<span class="badge badge-success">Livr. en cours</span>'
    elsif self.state == "livrée"
      '<span class="badge badge-info">livrée</span>'
    elsif self.state =="facturée"
      '<span class="badge badge-primary">Facturée</span>'
    elsif self.state == "annulée"
      '<span class="badge badge-dark">Annulée</span>'
    elsif self.state == "refusée"
      '<span class="badge badge-danger">Refusée</span>'
    end
  end
  
  
  after_create do
    self.state = "brouillon" if !self.imported
    self.save
  end
  #["brouillon", "offre", "commande", "livrée","facturée", "annulée", "refusée"]
  
  def state_ok?
    self.state == "facturée" ? true : false
    
  end
  def generate_d
    
    if self.p_documents.create(:p_document_type => PDocumentType.find_by_label("Devis"))
      self.state = "offre"
      self.save
    end
  end
  
  def generate_fp
    
    if self.p_documents.create(:p_document_type => PDocumentType.find_by_label("Facture proforma"))
      self.state = "offre"
      self.save
    end
    
  end
  
  def generate_bc
    
    if self.p_documents.create(:p_document_type => PDocumentType.find_by_label("Bon de commande"))
      self.state = "commande"
      self.save
    end
    
  end
  
  def generate_bl(sheet_id=nil)
    
    if self.p_documents.create(:sheet_id => sheet_id, :p_document_type => PDocumentType.find_by_label("Bon de livraison"))
      #self.state = "livrée"
      self.save
    end
    
  end
  
  def generate_f
    
    if self.p_documents.create(:p_document_type => PDocumentType.find_by_label("Facture"))
      self.state = "facturée"
      self.save
    end
    
  end
  
  def document_date(document=nil)
    self.created_at
  end
  
  STATES = ["brouillon", "offre", "commande", "livrée","facturée", "annulée", "refusée"]
end
