Browse Source

advanced search in 7 places

chemi ledon 7 years atrás
parent
commit
360becdb9b

+ 31 - 11
app/datatables/available_products_datatable.rb

@@ -67,24 +67,44 @@ class AvailableProductsDatatable
       end
 
     products = products.page(page).per_page(per_page)
+
     search = params[:busqueda]
-    name_searched =
-      if search.include? ':'
-        search[0, search.index(':') - 1]
-      else
-        search
-      end
+
     unless search.blank?
-      products = products.where("products.sku ilike :search or products.name ilike :search", search: "%#{name_searched}%").order('products.name')
-      if search.include? ':'
-        attribute = search[search.index(':') + 1, search.length]
-        products = products.where('products.attributes_json ilike :attribute', attribute: "%#{attribute}%")
-      end
+      products = products.where(prepare_query_string(search)).order('products.name')
     end
 
     products
   end
 
+  def prepare_query_string(query)
+    product_query =
+      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|
+          next if attribute.nil?
+          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 products.attributes_json::json->>'#{attr_type}' ilike '%#{attribute}%'")
+        end
+        "products.sku ilike '%#{@product_name}%' or products.name ilike '%#{@product_name}%' #{attrs_query_string}"
+      else
+        "products.sku ilike '%#{query}%' or products.name ilike '%#{query}%'"
+      end
+    product_query
+  end
+
   def page
     params[:start].to_i / per_page + 1
   end

+ 33 - 4
app/datatables/stock_by_pointsale_datatable.rb

@@ -29,7 +29,7 @@ class StockByPointsaleDatatable
           (available.product.category.parent_id.zero? ? available.product.category.category : available.product.category.parent.category),
           (available.product.category.parent_id != 0 ? available.product.category.category : ' '),
           available.stock.round,
-          "<h3> $#{get_price_sale(available.product)} </h3>"
+          "<h3> #{get_price_sale(available.product)} </h3>"
         ].compact
       else
         arr = [
@@ -88,13 +88,42 @@ class StockByPointsaleDatatable
       stock = stock.page(page).per_page(per_page)
     end
 
-    unless params[:busqueda].blank?
-      stock = stock.where("products.sku ilike :search or products.name ilike :search", search: "%#{params[:busqueda]}%")
-    end
+    search = params[:busqueda]
 
+    unless search.blank?
+      stock = stock.where(prepare_query_string(search))
+    end
     stock
   end
 
+  def prepare_query_string(query)
+    product_query =
+      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|
+          next if attribute.nil?
+          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 products.attributes_json::json->>'#{attr_type}' ilike '%#{attribute}%'")
+        end
+        "products.sku ilike '%#{@product_name}%' or products.name ilike '%#{@product_name}%' #{attrs_query_string}"
+      else
+        "products.sku ilike '%#{query}%' or products.name ilike '%#{query}%'"
+      end
+    product_query
+  end
+
   def page
     params[:start].to_i / per_page + 1
   end

+ 34 - 1
app/datatables/stocks_datatable.rb

@@ -54,10 +54,43 @@ class StocksDatatable
         AvailableProduct.activos.includes(:product, :categories).where(pointsale_id: @current_user.pointsale_id.present? ? @current_user.pointsale_id : params[:pointsale]).order('products.name')
       end
     stock = stock.page(page).per_page(per_page)
-    stock = stock.where("products.sku ilike :search or products.name ilike :search", search: "%#{params[:busqueda]}%").order('products.name') unless params[:busqueda].blank?
+
+    search = params[:busqueda]
+
+    unless search.blank?
+      stock = stock.where(prepare_query_string(search)).order('products.name')
+    end
     stock
   end
 
+  def prepare_query_string(query)
+    product_query =
+      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|
+          next if attribute.nil?
+          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 products.attributes_json::json->>'#{attr_type}' ilike '%#{attribute}%'")
+        end
+        "products.sku ilike '%#{@product_name}%' or products.name ilike '%#{@product_name}%' #{attrs_query_string}"
+      else
+        "products.sku ilike '%#{query}%' or products.name ilike '%#{query}%'"
+      end
+    product_query
+  end
+
   def page
     params[:start].to_i / per_page + 1
   end

+ 2 - 6
app/views/cash_outs/_form.html.erb

@@ -49,7 +49,7 @@
           <div class="col-md-8">
             <div class="input-group">
               <span class="input-group-addon"> $ </span>
-              <%= f.text_field :received_cash, {:class=>"form-control mask_decimal", :oninput => "receivedAndFundCash()", :disabled => true} %>
+              <%= f.text_field :received_cash, {:class=>"form-control mask_decimal", :onkeyup => "receivedAndFundCash()", :disabled => true} %>
             </div>
           </div>
         </div>
@@ -255,12 +255,8 @@
         $('#cash_out_received_cash').attr('disabled', true);
         $('#cash_out_received_cash').val('');
       }
-    });
-
-    $('#cash_out_physical_cash').keyup(function() {
       receivedAndFundCash();
     });
-
   });
 
   function addCashOut() {
@@ -284,7 +280,7 @@
 
         if(receivedCash > physicalCash) {
           toastr["error"]("El retiro de efectivo no puede ser mayor al efectivo disponible en caja.");
-          $('#submit_cash_out').addClass('disabled');
+          $('#submit_cash_out').attr('disabled', true);
           $('#cash_out_cash_fund').val('');
           $('#cash_fund_display').val('');
         } else {