|
|
@@ -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
|
|
|
|