class PProductRef < ApplicationRecord
  belongs_to :p_product

  has_many :p_customer_cats, :through => :p_product

  has_many :p_product_features
  accepts_nested_attributes_for :p_product_features, allow_destroy: true

  has_many :p_product_ref_price_histories

  belongs_to :p_product_color

  has_many :p_articles
  has_many :p_article_serial_nums, through: :p_articles
  accepts_nested_attributes_for :p_article_serial_nums, allow_destroy: true

  has_many :p_product_ref_specs
  accepts_nested_attributes_for :p_product_ref_specs, allow_destroy: true

  #validates :ct_price_ht, :presence => true
  

  has_many :p_customer_product_prices, :dependent => :destroy
  accepts_nested_attributes_for :p_customer_product_prices, allow_destroy: true

  scope :enableds, -> {where(:enabled => true)}

  scope :assembleds, -> {where(:assembled => true)}

  scope :not_assembleds, -> {where(:assembled => false)}


  has_many :p_product_assembleds, :foreign_key => :parent_product_ref_id
  accepts_nested_attributes_for :p_product_assembleds, allow_destroy: true

  before_validation :update_new_product
  
  acts_as_sorting :fields => {
    :id => {:name => "Id", :reorder => true},
    :p_product_cat => {:name => "Catégorie", :reorder => true},
    :ref => {:name => "Référence"},
    :cc_name => {:name => "Produit"},
    :description => {:name => "Description"},
    :ct_sub_name => {:name => "Nom référence"},
    :ct_price_ht => {:name => "Prix de vente", :as => :currency},
    
    
    :actions => {:name => "Actions", :reorder => false}
  }
  

  def not_imported?
    if !self.p_product or !self.p_product.imported
      true
    else
      false
    end
  end

  before_save do
    puts self.ct_price_ht_changed?
    if self.ct_price_ht_changed? and self.id
      self.p_product_ref_price_histories.create(:price => self.ct_price_ht)
    end
  end


  def self.for_search(search)
    PProductRef.joins(:p_product).where("p_products.code LIKE ? or p_product_refs.uv LIKE ? or cc_name LIKE ? or ref LIKE ? or cc_cat_name LIKE ? or cc_sub_cat_names  LIKE ? ", "%#{search}%","%#{search}%", "%#{search}%", "%#{search}%", "%#{search}%", "%#{search}%")
  end


  def ca_name
    if self.p_product
      self.p_product.name + " - " + self.ct_sub_name
    end
  end

  def ca_code
    self.p_product.code if self.p_product
  end

  def ca_p_product_cat_id
    self.p_product.p_product_cat_id
  end

  QI_DYNAMICS = %w(name code p_product_cat_id sub_cat_names cat_name)

  eval(QI_DYNAMICS_CORE)

  before_validation :qi_dynamics_cache

  def get_price(options)
    p_customer = PCustomer.find(options[:p_customer_id]) if options[:p_customer_id]
    qte = options[:qte] if options[:qte]

    if self.ct_price_ht
      return self.ct_price_ht
    elsif self.p_product.ct_price_ht
      return self.p_product.ct_price_ht
    else
      return 0.0
    end
  end

  def ca_sub_cat_names
    r = []
    if self.p_product
      self.p_product.p_product_sub_cats.each do |ppsc|
        r << ppsc.name

      end

      r.join(" | ")
    else
      ""
    end
  end

  def ca_cat_name

    if self.p_product and self.p_product.p_product_cat
      puts self.p_product.p_product_cat.name

      self.p_product.p_product_cat.name
    else
      ""
    end

  end

  def update_new_product
    self.stocked = self.p_product.p_product_cat.stocked if self.p_product && self.p_product.p_product_cat
  end
  
  def get_price_for_shop(p_customer)
    if p_customer_product_price = self.p_customer_product_prices.where(:p_customer_id => p_customer.id).first and p_customer_product_price.price?
      p_customer_product_price.price


    else
      
      return self.get_price(:p_customer_id => nil, :qte => 1)
      
  
    end
  end
  
  def member_label
    "#{self.ref} | #{self.p_product.name} - #{self.ct_sub_name} - #{self.p_product_color.name if self.p_product_color} | #{self.cat_name}"
  end

end
