class PProduct < ApplicationRecord
  has_many :p_product_p_normes, :dependent => :destroy
  has_many :p_product_specs, :through => :p_product_p_normes

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

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


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

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

  has_many :p_articles, through: :p_product_refs

  # has_many :p_product_ref_specs, through: :p_product_refs
  # accepts_nested_attributes_for :p_product_ref_specs
  # # has_many :p_spec_types, through: :p_product_ref_specs
  # has_many :p_spec_values, through: :p_product_ref_specs
  # accepts_nested_attributes_for :p_spec_types, allow_destroy: true
  # accepts_nested_attributes_for :p_spec_values, allow_destroy: true


  has_many :p_product_images

  belongs_to :p_customer


  belongs_to :p_product_cat
  belongs_to :s_brand

  validates :name, :presence => true
  validates :code, :presence => true, :uniqueness => true


  has_many :tvable_tva_rates, :as => :tvable
  has_many :tva_rates, :through => :tvable_tva_rates


  has_many :p_product_p_product_sub_cats, :dependent => :destroy

  has_many :p_product_sub_cats, :through => :p_product_p_product_sub_cats

  has_many :p_product_p_customer_cats, :dependent => :destroy

  has_many :p_customer_cats, :through => :p_product_p_customer_cats

  has_many :p_product_origines

  has_many :p_origines, :through => :p_product_origines

  has_many :p_product_utilisations

  has_many :p_utilisations, :through => :p_product_utilisations


  acts_as_sorting :fields => {
      :id => {:name => "ID"},
      :code => {:name => "Code", :reorder => true},
      :name => {:name => "Nom", :reorder => true},
      :enabled => {:name => "Actif ?", :reorder => true, :format => :boolean},
      :p_product_cat => {:name => "Catégorie", :reorder => false},
      :s_brand => {:name => "Marque", :reorder => true, :sort_name => "s_brands.name", :member_label => :name},
      
      :actions => {:name => "Actions", :reorder => true}

    }
   



  def self.for_search(search)
    PProduct.where("name LIKE ? or code LIKE ?", "%#{search}%", "%#{search}%").where(:enabled => true)
  end

  def self.convert_price_kg
    PProduct.where(:price_calc => "Kg").all.each do |p_product|
      weight = nil

      p_product.p_product_refs.each do |p_product_ref|
        if p_product_ref.weight.to_f != 0.0
          weight = p_product_ref.weight
          p_product_ref.ct_price_ht = weight * p_product_ref.ct_price_ht

          p_product_ref.save
        end



      end

      if weight
        p_product.ct_purchase_price_ht = p_product.ct_purchase_price_ht*weight
      end

      p_product.save

    end
  end

  after_create do


  end

  after_save do
    self.p_product_refs.each do |ppr|
      ppr.save
    end
  end


  def tva_rate(accounting_zone_id)
    if r = self.tva_rates.where(:accounting_zone_id => accounting_zone_id).first

    elsif self.p_product_cat and r = self.p_product_cat.tva_rates.where(:accounting_zone_id => accounting_zone_id).first

    else
      r = nil

    end

    return r
  end


  def csv_tva_rate
    self.tva_rate(1).rate if self.tva_rate(1)
  end


  def csv_dluo
    ""
  end

  def csv_enabled
    if self.enabled
      "Oui"
    else
      "Non"
    end
  end

  def csv_p_product_cat
    self.p_product_cat.name if self.p_product_cat
  end

  def csv_p_customer_cats
    self.p_customer_cats.map{ |p| p.name}.join(",")
  end

  def csv_s_brand
    self.s_brand.name if self.s_brand
  end

  def default_image
    self.p_product_images.order(:position).first
  end

end
