class PProduct < ActiveRecord::Base
  
  belongs_to :image_file
  belongs_to :p_product_cat
  

  belongs_to :p_origine
  
  belongs_to :p_ep
  
  has_many :p_product_images
  
  has_many :p_product_colors, :dependent => :destroy
  has_many :p_colors, :through => :p_product_colors
  
  has_many :p_product_certifs, :dependent => :destroy
  has_many :p_certifs, :through => :p_product_certifs
  
  has_many :p_product_sizes, :dependent => :destroy
  has_many :p_sizes, :through => :p_product_sizes
  
  has_many :p_product_qtes, :dependent => :destroy
  has_many :p_qtes, :through => :p_product_qtes
  
  
  has_many :p_product_stocks, :dependent => :destroy
  accepts_nested_attributes_for :p_product_stocks
  
  has_many :p_degressifs, :dependent => :destroy
  accepts_nested_attributes_for :p_degressifs, :allow_destroy => true
  
  
 
  scope :enableds, -> {where(:archived => false, :enabled => true)}
  
  
  def default_image
    self.p_product_images.order(:position).first
  end
  
  
  def get_prices(p_customer=nil)
    if p_customer
      p_price_cat = p_customer.p_price_cat
    else
      p_price_cat = PPriceCat.first
    end
    self.p_degressifs.joins(:p_price_cats).where(:p_price_cats => {:id => p_price_cat.id}).order("nbr ASC").all
  end
  
  after_save do
    self.generate_stock
    
  end
  
  after_create do
    self.generate_degressif
  end
  
  
  def generate_degressif
    vals = [0]#[100,200,300,400,500,1000,3000,5000,10000]
    
    vals.each do |val|
    
      self.p_degressifs.create(:nbr => val, :p_price_cat_ids => PPriceCat.all.ids)
      
    end
    
    
  end

  
  def self.for_search(search)
    PProduct.where(:archived => false).where("name LIKE ? or code LIKE ?", "%#{search}%", "%#{search}%")
  end
  
  after_create do
    
  end
  
  
  def generate_stock
    if !self.archived
      ids_saveds = []
      ids_new_saveds = []
    
      if self.p_sizes.count > 0
      
        self.p_sizes.each do |p_size|
          if self.p_colors.count > 0
          
            self.p_colors.each do |p_color|
              if !(p_product_stock = self.p_product_stocks.where(:p_size_id => p_size.id, :p_color_id => p_color.id).first)
          
                p_product_stock = self.p_product_stocks.create(:p_size_id => p_size.id, :p_color_id => p_color.id, :stock_th_ok => 0, :stock_ok => 0)
          
                ids_new_saveds << p_product_stock.id
              end
        
              ids_saveds << p_product_stock.id

            end
    
          else
      
      
            if !(p_product_stock = self.p_product_stocks.where(:p_color_id => nil, :p_size_id => p_size.id).first)
          
              p_product_stock = self.p_product_stock.create(:p_size_id => p_size.id, :stock_th_ok => 0, :stock_ok => 0)
          
              ids_new_saveds << p_product_stock.id
            end
        
            ids_saveds << p_product_stock.id
      
          end
        
        end
    
      else
      
        if self.p_colors.count > 0
      
          self.p_colors.each do |p_color|
          
            if !(p_product_stock = self.p_product_stocks.where(:p_color_id => p_color.id, :p_size_id => nil).first)
          
              p_product_stock = self.p_product_stocks.create(:p_color_id => p_color.id, :stock_th_ok => 0, :stock_ok => 0)
          
              ids_new_saveds << p_product_stock.id
            end
        
            ids_saveds << p_product_stock.id
          
          end
    
    
    
        else
        
          if !(p_product_stock = self.p_product_stocks.where(:p_color => nil, :p_size_id => nil).first)
          
            p_product_stock = self.p_product_stocks.create(:stock_th_ok => 0, :stock_ok => 0)
          
            ids_new_saveds << p_product_stock.id
          end
        
          ids_saveds << p_product_stock.id
        
      
        end
      
      
      
      end
    
      self.p_product_stocks.where("id not IN (?)", ids_saveds).destroy_all
    end
  end
  
  
end
