From 43e26cabf232b4c62ce4e91bf13a58d143021a14 Mon Sep 17 00:00:00 2001
From: Philippe
Date: Thu, 18 Nov 2021 11:54:07 +0100
Subject: [PATCH] fixing available_p_articles method
---
app/models/p_article.rb | 21 ++++++++++++++++++---
1 file changed, 18 insertions(+), 3 deletions(-)
diff --git a/app/models/p_article.rb b/app/models/p_article.rb
index 9348933..f2da272 100644
--- a/app/models/p_article.rb
+++ b/app/models/p_article.rb
@@ -62,14 +62,29 @@ class PArticle < ApplicationRecord
"#{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|
- if LineStockUsage.where(p_article_id: p_article.id).empty?
- p_articles.drop(p_article.id)
+ #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 p_articles
+
+ return PArticle.where(id: available_p_articles_ids)
end
end