From 7b223648b809795e980ff3c5c94f588d983a016a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A9?= Date: Wed, 29 Sep 2021 15:45:28 +0200 Subject: [PATCH] p_product_ref search + filter ok --- .../admin/p_product_refs_controller.rb | 31 ++++++++++++++----- .../admin/p_product_refs/index.html.haml | 15 ++++++--- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/app/controllers/admin/p_product_refs_controller.rb b/app/controllers/admin/p_product_refs_controller.rb index 1ee4dde..641f43b 100644 --- a/app/controllers/admin/p_product_refs_controller.rb +++ b/app/controllers/admin/p_product_refs_controller.rb @@ -16,9 +16,15 @@ class Admin::PProductRefsController < ApplicationController @p_product_refs = PProductRef.joins(:p_product) - @purchase_p_product_cats = PProductCat.where(:purchase => true).order(:name).all + # @purchase_p_product_cats = PProductCat.where(:purchase => true).order(:name).all + global_search_ids = @p_product_refs.ids + p_product_ref_cat_id_ids = @p_product_refs.ids + s_brand_id_ids = @p_product_refs.ids + if params[:search][:global].to_s != "" + global_search_ids = PProductRef.global_search(params[:search][:global]).ids + end if params[:search][:code].to_s != "" @p_product_refs = @p_product_refs.where("p_product_refs.ref LIKE ?","#{params[:search][:code]}%") @@ -31,19 +37,27 @@ class Admin::PProductRefsController < ApplicationController if params[:search][:p_product_ref_cat_id].to_s != "" if params[:search][:p_product_ref_cat_id].to_s == "null" - @p_product_refs = @p_product_refs.where(:cc_p_product_cat_id => nil) + # @p_product_refs = @p_product_refs.where(:cc_p_product_cat_id => nil) + p_product_ref_cat_id_ids = @p_product_refs.where(:cc_p_product_cat_id => nil).ids else - @p_product_refs = @p_product_refs.where(:cc_p_product_cat_id => params[:search][:p_product_ref_cat_id]) + # @p_product_refs = @p_product_refs.where(:cc_p_product_cat_id => params[:search][:p_product_ref_cat_id]) + p_product_ref_cat_id_ids = @p_product_refs.where(:cc_p_product_cat_id => params[:search][:p_product_ref_cat_id]).ids end end if params[:search][:s_brand_id].to_s != "" - @p_product_refs = @p_product_refs.where(:p_products => {:s_brand_id => params[:search][:s_brand_id]}) + # @p_product_refs = @p_product_refs.where(:p_products => {:s_brand_id => params[:search][:s_brand_id]}) + # @p_product_refs = @p_product_refs.select { |ref| ref.p_product.s_brand_id == params[:search][:s_brand_id]} + s_brand_id_ids = @p_product_refs.where(:p_products => {:s_brand_id => params[:search][:s_brand_id]}).ids end - if params[:search][:global].to_s != "" - @p_product_refs = PProductRef.global_search(params[:search][:global]) + final_ids = global_search_ids & p_product_ref_cat_id_ids & s_brand_id_ids + + if params[:search].present? + @p_product_refs = PProductRef.where(id: final_ids) + else + @p_product_refs = PProductRef.joins(:p_product) end @p_product_refs = sort_by_sorting(@p_product_refs, "id DESC") @@ -110,7 +124,8 @@ class Admin::PProductRefsController < ApplicationController end def autocomplete - @p_product_refs = PProductRef.for_search(params[:search]).limit(50) + @p_product_refs = PProductRef.joins(:p_product).all + # @p_product_refs = PProductRef.for_search(params[:search]).limit(50) if params[:p_customer_id] @p_customer = PCustomer.where(:id => params[:p_customer_id]).first @@ -196,5 +211,5 @@ class Admin::PProductRefsController < ApplicationController @p_product_ref = PProductRef.find(params[:id]) end - + end diff --git a/app/views/admin/p_product_refs/index.html.haml b/app/views/admin/p_product_refs/index.html.haml index bc2bd0f..f301f4e 100644 --- a/app/views/admin/p_product_refs/index.html.haml +++ b/app/views/admin/p_product_refs/index.html.haml @@ -36,13 +36,20 @@ %td Catégorie : %td - =select_tag "search[p_product_ref_cat_id]", options_for_select([["",""],["Aucune","null"]]+PProductCat.order(:name).all.map{|a| [a.name, a.id]}, params[:search][:p_product_ref_cat_id]), class: "custom-select" - + .input-group + =select_tag "search[p_product_ref_cat_id]", options_for_select([["",""],["Aucune","null"]]+PProductCat.order(:name).all.map{|a| [a.name, a.id]}, params[:search][:p_product_ref_cat_id]), class: "custom-select" + .input-group-append{:onclick => "$(this).prev('select').val('').prop('selectedIndex',0);"} + .btn.btn-outline-dark + =ic(:times) + %td Marque : %td - =select_tag "search[s_brand_id]", options_for_select([["",""]]+SBrand.order(:name).all.map{|a| [a.name, a.id]}, params[:search][:s_brand_id]), class: "custom-select" - + .input-group + =select_tag "search[s_brand_id]", options_for_select([["",""]]+SBrand.order(:name).all.map{|a| [a.name, a.id]}, params[:search][:s_brand_id]), class: "custom-select" + .input-group-append{:onclick => "$(this).prev('select').val('').prop('selectedIndex',0);"} + .btn.btn-outline-dark + =ic(:times) =render :partial => "qi/qi_ordered_table_search_footer", :locals => {:collection_object => @p_product_refs}