available_products_datatable.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. class AvailableProductsDatatable
  2. delegate :params, :fa_icon, :available_products_path, 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: @pointsale.products.activos_children.size,
  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. category = available.product.categories[0]
  20. {
  21. 'DT_RowId' => "available_#{available.id}",
  22. '0' => '<input class="form-control checkboxes" type="checkbox"/>',
  23. '1' => available.product.sku,
  24. '2' => get_display_name(available.product),
  25. '3' => (category.parent_id.zero? ? category.category : category.parent.category),
  26. '4' => (category.parent_id != 0 ? category.category : ' '),
  27. '5' => available.stock
  28. }.compact.reject { |_k, v| v.nil? }
  29. end
  30. else
  31. products.map do |product|
  32. category = product.categories[0]
  33. {
  34. 'DT_RowId' => "product_#{product.id}",
  35. '0' => '<input class="form-control checkboxes" type="checkbox"/>',
  36. '1' => product.sku,
  37. '2' => get_display_name(product),
  38. '3' => (category.parent_id.zero? ? category.category : category.parent.category),
  39. '4' => (category.parent_id != 0 ? category.category : ' ')
  40. }.compact.reject { |_k, v| v.nil? }
  41. end
  42. end
  43. end
  44. def get_display_name(product)
  45. name = "<label style='margin-bottom:0px'><strong>#{product.name}</strong></label> <br>"
  46. name += "#{product.display_attributes} <br>" if product.display_attributes.present?
  47. name += "<i class='fa fa-barcode'></i>: #{product.barcode} <br>" if product.barcode.present?
  48. name += "Descripción: #{product.description}" if product.barcode.present?
  49. name
  50. end
  51. def products
  52. @products ||= fetch_products
  53. end
  54. def fetch_products
  55. products =
  56. if params[:table] == 'in_pointsale'
  57. AvailableProduct.joins(:product, :categories).activos.where(pointsale_id: params[:id]).activos
  58. else
  59. products_in_pointsale = @pointsale.products.activos_children.pluck(:id)
  60. Product.joins(:categories).activos_children.where.not(id: products_in_pointsale).order('products.name')
  61. end
  62. products = products.page(page).per_page(per_page)
  63. search = params[:busqueda]
  64. name_searched =
  65. if search.include? ':'
  66. search[0, search.index(':') - 1]
  67. else
  68. search
  69. end
  70. unless search.blank?
  71. products = products.where("products.sku ilike :search or products.name ilike :search", search: "%#{name_searched}%").order('products.name')
  72. if search.include? ':'
  73. attribute = search[search.index(':') + 1, search.length]
  74. products = products.where('attributes_json ilike :attribute', attribute: "%#{attribute}%")
  75. end
  76. end
  77. products
  78. end
  79. def page
  80. params[:start].to_i / per_page + 1
  81. end
  82. def per_page
  83. params[:length].to_i > 0 ? params[:length].to_i : 20
  84. end
  85. def sort_column
  86. columns = %w[name]
  87. columns[params[:iSortCol_0].to_i]
  88. end
  89. def sort_direction
  90. params[:sSortDir_0] == "desc" ? "desc" : "asc"
  91. end
  92. end