class PProductRef < ApplicationRecord belongs_to :p_product belongs_to :sorecop_cat has_many :sorecop_taxes, through: :sorecop_cat has_one :s_brand, through: :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 :line_stocks 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, dependent: :destroy accepts_nested_attributes_for :p_product_ref_specs, allow_destroy: true, reject_if: :all_blank #validates :ct_price_ht, :presence => true validate :ean, :is_integer has_many :p_customer_product_prices, :dependent => :destroy accepts_nested_attributes_for :p_customer_product_prices, allow_destroy: true has_many :p_fournisseur_refs 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, :sort_name => "cc_p_product_cat_id",}, :ref => {:name => "Référence"}, # :cc_name => {:name => "Produit"}, # :description => {:name => "Description"}, :cc_name => {:name => "Nom référence"}, :ct_price_ht => {:name => "Prix de vente", :as => :currency}, :ean => {:name => "EAN"}, :deee => {:name => "DEEE", :as => :currency}, # :sorecop => {:name => "Sorecop", :as => :currency}, # :sorecop_comment => {:name => "Commentaire Sorecop"}, :actions => {:name => "Actions", :reorder => false} } acts_as_caching :fields => [:sorecop, :deee, :name, :code, :p_product_cat_id] include PgSearch::Model pg_search_scope :global_search, against: [:ref, :cc_name], associated_against: { s_brand: [ :code, :name ], p_product: [:code], p_product_color: [:name, :color] }, using: { tsearch: { prefix: true } } attr_accessor :price_line_id after_create do if self.price_line_id pl = PriceLine.find(self.price_line_id) pl.update(p_product_ref: self) end end after_save do PProduct.destroy_temp_p_product end 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 storage = self.p_product_ref_specs.where(p_spec_type: PSpecType.find_by(name: "Stockage")).first cellular = self.p_product_ref_specs.where(p_spec_type: PSpecType.find_by(name: "Connectivité")).first screen = self.p_product_ref_specs.where(p_spec_type: PSpecType.find_by(name: "Résolution")).first power = self.p_product_ref_specs.where(p_spec_type: PSpecType.find_by(name: "Puissance éléctrique")).first geoloc = self.p_product_ref_specs.where(p_spec_type: PSpecType.find_by(name: "Géolocalisation")).first sim = self.p_product_ref_specs.where(p_spec_type: PSpecType.find_by(name: "Sim")).first size = self.p_product_ref_specs.where(p_spec_type: PSpecType.find_by(name: "Taille")).first version = self.p_product_ref_specs.where(p_spec_type: PSpecType.find_by(name: "Version ROM")).first arr = [] arr << self.p_product.name if self.p_product arr << self.ct_sub_name arr << storage.p_spec_value.member_label if storage arr << cellular.p_spec_value.member_label if cellular arr << screen.p_spec_value.member_label if screen arr << power.p_spec_value.member_label if power arr << geoloc.p_spec_value.member_label if geoloc arr << sim.p_spec_value.member_label if sim arr << size.p_spec_value.member_label if size arr << version.p_spec_value.member_label if version arr << self.p_product_color.name if p_product_color arr.compact.each{|el| el.strip!}.join(' ') # 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.cc_name}" end def ca_sorecop return ct_sorecop if ct_sorecop return "Pas de catégorie Sorecop" if self.p_product.sorecop_cat.blank? # if self.cc_sorecop and self.cc_sorecop > 0.0 # return self.cc_sorecop # else find_sorecop_tax # end end def find_sorecop_tax p_product_ref_spec = self.p_product_ref_specs.where(p_spec_type: PSpecType.find_by_name("Stockage")).first return "Pas de stockage" if p_product_ref_spec.blank? storage_capacity = p_product_ref_spec.p_spec_value.value.to_f if p_product_ref_spec.p_spec_value.unit.casecmp?("Gb") tax = self.p_product.sorecop_cat.sorecop_taxes.where('critere_min < ? AND critere_max >= ?', storage_capacity, storage_capacity).first elsif p_product_ref_spec.p_spec_value.unit.casecmp?("Tb") tax = self.p_product.sorecop_cat.sorecop_taxes.where('critere_min < ? AND critere_max >= ?', storage_capacity * 1000, storage_capacity * 1000).first elsif p_product_ref_spec.p_spec_value.unit.casecmp?("Mb") tax = self.p_product.sorecop_cat.sorecop_taxes.where('critere_min < ? AND critere_max >= ?', storage_capacity / 1000, storage_capacity / 1000).first end if tax.fixed_price tax.price else tax.price * storage_capacity end end def ca_deee if ct_deee return ct_deee end if self.cc_deee return self.cc_deee else "DEEE à renseigner" end end def is_integer if ean.present? && ean.match(/\A[+-]?\d+\z/).nil? errors.add(:ean, "ne doit être composé que de chiffres") end end end