available_products_datatable.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. name_searched =
  64. if search.include? ':'
  65. search[0, search.index(':') - 1]
  66. else
  67. search
  68. end
  69. unless search.blank?
  70. products = products.where("products.sku ilike :search or products.name ilike :search", search: "%#{name_searched}%").order('products.name')
  71. if search.include? ':'
  72. attribute = search[search.index(':') + 1, search.length]
  73. products = products.where('products.attributes_json ilike :attribute', attribute: "%#{attribute}%")
  74. end
  75. end
  76. products
  77. end
  78. def page
  79. params[:start].to_i / per_page + 1
  80. end
  81. def per_page
  82. params[:length].to_i > 0 ? params[:length].to_i : 20
  83. end
  84. def sort_column
  85. columns = %w[name]
  86. columns[params[:iSortCol_0].to_i]
  87. end
  88. def sort_direction
  89. params[:sSortDir_0] == "desc" ? "desc" : "asc"
  90. end
  91. end