available_products_datatable.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. class AvailableProductsDatatable
  2. delegate :params, :fa_icon, :available_products_path, :number_to_currency, to: :@view
  3. def initialize(view, pointsale)
  4. @view = view
  5. @pointsale = pointsale
  6. end
  7. def as_json(*)
  8. {
  9. sEcho: params[:sEcho].to_i,
  10. iTotalRecords: params[:table] == 'in_pointsale' ? @pointsale.products.activos_children.size : products.total_entries,
  11. iTotalDisplayRecords: products.total_entries,
  12. aaData: data
  13. }
  14. end
  15. private
  16. def data
  17. if params[:table] == 'in_pointsale'
  18. products.map do |available|
  19. {
  20. 'DT_RowId' => "available_#{available.id}",
  21. '0' => '<input class="form-control checkboxes" type="checkbox"/>',
  22. '1' => available.product.sku,
  23. '2' => get_display_name(available.product),
  24. '3' => (available.product.category.parent_id.zero? ? available.product.category.category : available.product.category.parent.category),
  25. '4' => (available.product.category.parent_id != 0 ? available.product.category.category : ' '),
  26. '5' => available.stock
  27. }.compact.reject { |_k, v| v.nil? }
  28. end
  29. else
  30. products.map do |product|
  31. {
  32. 'DT_RowId' => "product_#{product.id}",
  33. '0' => '<input class="form-control checkboxes" type="checkbox"/>',
  34. '1' => product.sku,
  35. '2' => get_display_name(product),
  36. '3' => (product.category.parent_id.zero? ? product.category.category : product.category.parent.category),
  37. '4' => (product.category.parent_id != 0 ? product.category.category : ' '),
  38. '5' => number_to_currency(product.price_sale, precision: 2)
  39. }.compact.reject { |_k, v| v.nil? }
  40. end
  41. end
  42. end
  43. def get_display_name(product)
  44. name = "<label style='margin-bottom:0px'><strong>#{product.name}</strong></label> <br>"
  45. name += "#{product.display_attributes} <br>" if product.display_attributes.present?
  46. name += "<i class='fa fa-barcode'></i>: #{product.barcode} <br>" if product.barcode.present?
  47. name += "Descripción: #{product.description}" if product.barcode.present?
  48. name
  49. end
  50. def products
  51. @products ||= fetch_products
  52. end
  53. def fetch_products
  54. products =
  55. if params[:table] == 'in_pointsale'
  56. AvailableProduct.joins(:product, :categories).activos.where(pointsale_id: params[:id]).activos
  57. else
  58. products_in_pointsale = @pointsale.products.activos_children.pluck(:id)
  59. Product.joins(:category).activos_children.where.not(id: products_in_pointsale).order('products.name')
  60. end
  61. products = products.page(page).per_page(per_page)
  62. search = params[:busqueda]
  63. unless search.blank?
  64. products = products.where(prepare_query_string(search)).order('products.name')
  65. end
  66. products
  67. end
  68. def prepare_query_string(query)
  69. product_query =
  70. if query.include? ':' # search with attributes
  71. query_array = query.split(':')
  72. @product_name = query_array[0]
  73. query_array.shift # delete the name of the product from the array to iterate the attributes
  74. attrs_query_string = ''
  75. query_array.each do |attribute|
  76. next if attribute.nil?
  77. attr_type =
  78. case attribute[0]
  79. when 'c'
  80. 'colors'
  81. when 't'
  82. 'sizes'
  83. when 'e'
  84. 'styles'
  85. end
  86. attribute[0] = "" # delete the attribute type character
  87. attrs_query_string.concat(" AND products.attributes_json::json->>'#{attr_type}' ilike '%#{attribute}%'")
  88. end
  89. "products.sku ilike '%#{@product_name}%' or products.name ilike '%#{@product_name}%' #{attrs_query_string}"
  90. else
  91. "products.sku ilike '%#{query}%' or products.name ilike '%#{query}%'"
  92. end
  93. product_query
  94. end
  95. def page
  96. params[:start].to_i / per_page + 1
  97. end
  98. def per_page
  99. params[:length].to_i > 0 ? params[:length].to_i : 20
  100. end
  101. def sort_column
  102. columns = %w[name]
  103. columns[params[:iSortCol_0].to_i]
  104. end
  105. def sort_direction
  106. params[:sSortDir_0] == "desc" ? "desc" : "asc"
  107. end
  108. end