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 validates :ct_price_ht, :presence => true validates :uv, :presence => true validates :weight, :presence => true 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 self.p_product.name if self.p_product end def ca_p_product_cat_id self.p_product.p_product_cat_id end QI_DYNAMICS = %w(name 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 end