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 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 def self.for_search(search) PProduct.where("name LIKE ? or code LIKE ?", "%#{search}%", "%#{search}%").where(:enabled => true) 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 end