class PArticle < ApplicationRecord belongs_to :p_grade belongs_to :p_product_ref belongs_to :tva_type has_one :p_product, through: :p_product_ref has_one :p_product_color, through: :p_product_ref has_one :s_brand, through: :p_product_ref has_many :p_article_serial_nums, dependent: :destroy # has_many :p_serial_num_values, through: :p_article_serial_nums has_many :price_line_p_articles has_many :price_lines, through: :price_line_p_articles accepts_nested_attributes_for :p_article_serial_nums has_many :line_stock_p_articles has_many :line_stocks, through: :line_stock_p_articles validates_presence_of :p_product_ref # has_many :p_product_ref_specs, through: :p_product_ref # accepts_nested_attributes_for :p_product_ref_specs if PGrade::ACTIVATED acts_as_sorting :fields => { :id => {:name => "id", :reorder => true}, :p_product_ref_ref => {:name => "Code ref", :reorder => true}, :p_product_ref => {:name => "Référence", :reorder => true}, :p_grade => {:name => "Grade", :reorder => true}, :color => {:name => "Couleur"}, :p_article_serial_nums => {:name => "N° identifiants"}, :actions => {:name => "Actions", :reorder => false}, } else acts_as_sorting :fields => { :id => {:name => "id", :reorder => true}, :p_product_ref_ref => {:name => "Code ref", :reorder => true}, :p_product_ref => {:name => "Référence", :reorder => true}, :color => {:name => "Couleur"}, :p_article_serial_nums => {:name => "N° identifiants"}, :actions => {:name => "Actions", :reorder => false}, } end include PgSearch::Model pg_search_scope :global_search, associated_against: { p_product_ref: [:cc_name, :ref], s_brand: [ :code, :name ], p_product_color: [:name, :color], p_article_serial_nums: [:value] }, using: { tsearch: { prefix: true } } def member_label "#{p_product_ref.cc_name}" end def serialized_name "#{self.p_product_ref.cc_name} #{self.p_article_serial_nums.map{|x| "#{x.p_serial_num_type.name}: #{x.value}"}.join(' / ')}" end # def self.available_articles # p_articles = PArticle.joins(:line_stocks).where("qte_available > ?", 0) # p_articles.each do |p_article| # if LineStockUsage.where(p_article_id: p_article.id).empty? # p_articles.drop(p_article.id) # end # end # return p_articles # end def self.available_articles p_articles = PArticle.joins(:line_stocks).where("qte_available > ?", 0) used_p_articles_ids = LineStockUsage.where.not(p_article_id: nil).pluck(:p_article_id)#.map{|ls| ls.p_article_id} available_p_articles_ids =[] p_articles.each do |p_article| #puts used_p_articles_ids.include?(p_article.id) if !used_p_articles_ids.include?(p_article.id) available_p_articles_ids << p_article.id end end return PArticle.where(id: available_p_articles_ids) end end