Browse Source

new advanced search by multiple attribute

jose miguel 7 years ago
parent
commit
93eea823db

+ 23 - 6
app/controllers/application_controller.rb

@@ -58,18 +58,35 @@ class ApplicationController < ActionController::Base
 
   def find_from_stock
     query = params[:query]
-    if query.include? ':'
-      # buscar con atributos
-      product_name = query[0, query.index(':') - 1]
-      attribute =  query[query.index(':') + 1, query.length]
+    if query.include? ':' # search with attributes
+      query_array = query.split(':')
+      product_name = query_array[0]
+      query_array.shift # delete the name of the product from the array to iterate the attributes
+      attrs_query_string = ''
+      query_array.each do |attribute|
+        if attribute.present?
+          attr_type = case attribute[0]
+                      when 'c'
+                        'colors'
+                      when 't'
+                        'sizes'
+                      when 'e'
+                        'styles'
+                      end
+          attribute[0] = "" # delete the attribute type character
+          attrs_query_string.concat(" AND attributes_json::json->>'#{attr_type}' ilike '%#{attribute}%'")
+        else
+          next
+        end
+      end
     else
       product_name = query
     end
 
     if current_user.usertype == 'S'
-      render json: query.include?(":") ? Warehouse.find(current_user.warehouse_id).products.name_sku_barcode_attribute_like(product_name, attribute).where("stock > 0").limit(30).to_json(methods: [:small_img, :display_attributes]) : Warehouse.find(current_user.warehouse_id).products.name_sku_barcode_like(params[:query]).where("stock > 0").limit(30).to_json(methods: [:small_img, :display_attributes])
+      render json: query.include?(":") ? Warehouse.find(current_user.warehouse_id).products.name_sku_barcode_multiple_attribute_like(product_name, attrs_query_string).where("stock > 0").limit(30).to_json(methods: [:small_img, :display_attributes]) : Warehouse.find(current_user.warehouse_id).products.name_sku_barcode_like(params[:query]).where("stock > 0").limit(30).to_json(methods: [:small_img, :display_attributes])
     else
-      render json: query.include?(":") ? Pointsale.find(current_user.pointsale_id).products.name_sku_barcode_attribute_like(product_name, attribute).where("stock > 0").limit(30).to_json(methods: [:small_img, :display_attributes]) : Pointsale.find(current_user.pointsale_id).products.name_sku_barcode_like(params[:query]).where("stock > 0").limit(30).to_json(methods: [:small_img, :display_attributes])
+      render json: query.include?(":") ? Pointsale.find(current_user.pointsale_id).products.name_sku_barcode_multiple_attribute_like(product_name, attrs_query_string).where("stock > 0").limit(30).to_json(methods: [:small_img, :display_attributes]) : Pointsale.find(current_user.pointsale_id).products.name_sku_barcode_like(params[:query]).where("stock > 0").limit(30).to_json(methods: [:small_img, :display_attributes])
     end
   end
 

+ 1 - 0
app/models/product.rb

@@ -56,6 +56,7 @@ class Product < ActiveRecord::Base
   scope :vigentes_parents, -> { where("status != 0 and parent_id IS NULL ").order(" status ASC, name ASC") }
   scope :name_sku_barcode_like, ->(name) { where("status = 1 and is_parent = false and (name ilike ? or sku ilike ? or barcode ilike ?)", "%#{name}%", "%#{name}%", "%#{name}%").order("name") }
   scope :name_sku_barcode_attribute_like, ->(name, attribute) { where("status = 1 and is_parent = false and (name ilike ? or sku ilike ? or barcode ilike ?) and attributes_json ilike ?", "%#{name}%", "%#{name}%", "%#{name}%", "%#{attribute}%").order("name") }
+  scope :name_sku_barcode_multiple_attribute_like, ->(name, attributes_string) { where("status = 1 and is_parent = false and (name ilike ? or sku ilike ? or barcode ilike ?) #{attributes_string}", "%#{name}%", "%#{name}%", "%#{name}%").order("name") }
   # para special_prices
   scope :name_sku_barcode_like_sp, ->(name) { where("status = 1 and is_parent = true and (name ilike ? or sku ilike ? or barcode ilike ?)", "%#{name}%", "%#{name}%", "%#{name}%").order("name") }
 

+ 3 - 2
app/views/sales/_form.html.erb

@@ -162,7 +162,8 @@
 						<span class="required">*</span>
 					</label>
 					<div class="col-md-4">
-						<input class="form-control" type="text" id="typeahead" data-option-url="/find_products_from_stock/%QUERY">
+					<!-- -->
+						<input class="form-control" type="text" id="typeahead">
 					</div>
 					<button id="products_new" disabled class="btn btn-success" type="button" onclick="addRow()"> Agregar <i class="fa fa-plus"></i> </button>
 				</div>
@@ -380,7 +381,7 @@
 		},
 		queryTokenizer: Bloodhound.tokenizers.whitespace,
 		remote: {
-			url: $('#typeahead').data('option-url'),
+			url: '/find_products_from_stock?query=%QUERY',
 			wildcard: '%QUERY'
 		}
 	});

+ 1 - 1
config/routes.rb

@@ -232,7 +232,7 @@ Rails.application.routes.draw do
   # para special_prices
   get 'find_products_sp/:query' => 'application#find_sp', :format => :json
   #
-  get 'find_products_from_stock/:query' => 'application#find_from_stock', :format => :json
+  get 'find_products_from_stock' => 'application#find_from_stock', :format => :json
   get 'find_from_stock_by_pointsale/:pointsale_id/:query' => 'application#find_from_stock_by_pointsale', :format => :json
 
   get "getcategories/:category_id" => "application#get_subcategories", :as => "get_subcategories", :format => :json