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 has_many :fournisseur_product_refs accepts_nested_attributes_for :fournisseur_product_refs, allow_destroy: true validates :ref, :presence => true, :uniqueness => true validates :ct_price_ht, :presence => true #validates :uv, :presence => true #validates :weight, :presence => true #scope :recents, -> {order("articles.published_at DESC, articles.created_at DESC")} 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 #scope :between, lambda { |start, stop| # after(start).before(stop) #} acts_as_sorting :fields => { :id => {:name => "Id"}, :p_product => {:name => "Produit"}, :actions => {:name => "Actions", :reorder => false} } before_save do if self.p_product and self.p_product.stocked self.stocked = true else self.stocked = false end 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) ppr = PProductRef.joins(:p_product).where("ref LIKE ? or 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}%", "%#{search}%") return ppr 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