available_products_datatable.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. 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. '5' => number_to_currency(product.price_sale, precision: 2)
  41. }.compact.reject { |_k, v| v.nil? }
  42. end
  43. end
  44. end
  45. def get_display_name(product)
  46. name = "<label style='margin-bottom:0px'><strong>#{product.name}</strong></label> <br>"
  47. name += "#{product.display_attributes} <br>" if product.display_attributes.present?
  48. name += "<i class='fa fa-barcode'></i>: #{product.barcode} <br>" if product.barcode.present?
  49. name += "Descripción: #{product.description}" if product.barcode.present?
  50. name
  51. end
  52. def products
  53. @products ||= fetch_products
  54. end
  55. def fetch_products
  56. products =
  57. if params[:table] == 'in_pointsale'
  58. AvailableProduct.joins(:product, :categories).activos.where(pointsale_id: params[:id]).activos
  59. else
  60. products_in_pointsale = @pointsale.products.activos_children.pluck(:id)
  61. Product.joins(:categories).activos_children.where.not(id: products_in_pointsale).order('products.name')
  62. end
  63. products = products.page(page).per_page(per_page)
  64. search = params[:busqueda]
  65. name_searched =
  66. if search.include? ':'
  67. search[0, search.index(':') - 1]
  68. else
  69. search
  70. end
  71. unless search.blank?
  72. products = products.where("products.sku ilike :search or products.name ilike :search", search: "%#{name_searched}%").order('products.name')
  73. if search.include? ':'
  74. attribute = search[search.index(':') + 1, search.length]
  75. products = products.where('attributes_json ilike :attribute', attribute: "%#{attribute}%")
  76. end
  77. end
  78. products
  79. end
  80. def page
  81. params[:start].to_i / per_page + 1
  82. end
  83. def per_page
  84. params[:length].to_i > 0 ? params[:length].to_i : 20
  85. end
  86. def sort_column
  87. columns = %w[name]
  88. columns[params[:iSortCol_0].to_i]
  89. end
  90. def sort_direction
  91. params[:sSortDir_0] == "desc" ? "desc" : "asc"
  92. end
  93. end