stocks_datatable.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. class StocksDatatable
  2. delegate :params, :image_tag, :available_products_path, :products_initial_stock_path, to: :@view
  3. def initialize(view, user, showcolumns)
  4. @view = view
  5. @current_user = user
  6. @showcolumns = showcolumns
  7. end
  8. def as_json(_options = {})
  9. total =
  10. if @current_user.usertype == 'S'
  11. WarehouseStock.activos.where(warehouse_id: @current_user.warehouse_id)
  12. else
  13. AvailableProduct.activos.where(pointsale_id: @current_user.pointsale_id.present? ? @current_user.pointsale_id : params[:pointsale])
  14. end
  15. {
  16. sEcho: params[:sEcho].to_i,
  17. iTotalRecords: total.size,
  18. iTotalDisplayRecords: stock.total_entries,
  19. aaData: data
  20. }
  21. end
  22. private
  23. def data
  24. stock.map do |available|
  25. img = available.product.img_product? ? available.product.img_product.url(:small) : "/images/small/missing.png"
  26. {
  27. 'DT_RowId' => "availableProduct_#{available.id}",
  28. '0' => '<input class="form-control checkboxes" type="checkbox"/>',
  29. '1' => (image_tag img),
  30. '2' => available.product.sku,
  31. '3' => available.product.name + '<br>' + available.product.display_attributes,
  32. '4' => (available.product.category.parent_id.zero? ? available.product.category.category : available.product.category.parent.category),
  33. '5' => (available.product.category.parent_id != 0 ? available.product.category.category : ' '),
  34. '6' => (@showcolumns == "minMax" ? available.stock_min : available.stock),
  35. '7' => (@showcolumns == "minMax" ? available.stock_max : nil)
  36. }.compact.reject { |_k, v| v.nil? }
  37. end
  38. end
  39. def stock
  40. @stock ||= fetch_stock
  41. end
  42. def fetch_stock
  43. stock =
  44. if @current_user.usertype == 'S'
  45. WarehouseStock.activos.includes(:product, :categories).where(warehouse_id: @current_user.warehouse_id).order('products.name')
  46. else
  47. AvailableProduct.activos.includes(:product, :categories).where(pointsale_id: @current_user.pointsale_id.present? ? @current_user.pointsale_id : params[:pointsale]).order('products.name')
  48. end
  49. stock = stock.page(page).per_page(per_page)
  50. search = params[:busqueda]
  51. unless search.blank?
  52. stock = stock.where(prepare_query_string(search)).order('products.name')
  53. end
  54. stock
  55. end
  56. def prepare_query_string(query)
  57. product_query =
  58. if query.include? ':' # search with attributes
  59. query_array = query.split(':')
  60. @product_name = query_array[0]
  61. query_array.shift # delete the name of the product from the array to iterate the attributes
  62. attrs_query_string = ''
  63. query_array.each do |attribute|
  64. next if attribute.nil?
  65. attr_type =
  66. case attribute[0]
  67. when 'c'
  68. 'colors'
  69. when 't'
  70. 'sizes'
  71. when 'e'
  72. 'styles'
  73. end
  74. attribute[0] = "" # delete the attribute type character
  75. attrs_query_string.concat(" AND products.attributes_json::json->>'#{attr_type}' ilike '%#{attribute}%'")
  76. end
  77. "products.sku ilike '%#{@product_name}%' or products.name ilike '%#{@product_name}%' #{attrs_query_string}"
  78. else
  79. "products.sku ilike '%#{query}%' or products.name ilike '%#{query}%'"
  80. end
  81. product_query
  82. end
  83. def page
  84. params[:start].to_i / per_page + 1
  85. end
  86. def per_page
  87. params[:length].to_i > 0 ? params[:length].to_i : 20
  88. end
  89. def sort_column
  90. columns = %w[name]
  91. columns[params[:iSortCol_0].to_i]
  92. end
  93. def sort_direction
  94. params[:sSortDir_0] == "desc" ? "desc" : "asc"
  95. end
  96. end