available_products_datatable.rb 2.9 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(options = {})
  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 == 0 ? 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 == 0 ? 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. if product.display_attributes.present?
  47. name += "#{product.display_attributes} <br>"
  48. end
  49. if product.barcode.present?
  50. name += "<i class='fa fa-barcode'></i>: #{product.barcode} <br>"
  51. end
  52. if product.description.present?
  53. name += "Descripción: #{product.description}"
  54. end
  55. return name
  56. end
  57. def products
  58. @products ||= fetch_products
  59. end
  60. def fetch_products
  61. if params[:table] == 'in_pointsale'
  62. products = AvailableProduct.activos.where(:pointsale_id => params[:id]).activos
  63. else
  64. products_in_pointsale = @pointsale.products.activos_children.pluck(:id)
  65. products = Product.activos_children.where.not(id: products_in_pointsale)
  66. end
  67. products = products.page(page).per_page(per_page)
  68. unless params[:busqueda].blank?
  69. products = products.where("products.sku ilike :search or products.name ilike :search", search: "%#{params[:busqueda]}%").order('products.name')
  70. end
  71. products
  72. end
  73. def page
  74. params[:start].to_i/per_page + 1
  75. end
  76. def per_page
  77. params[:length].to_i > 0 ? params[:length].to_i : 20
  78. end
  79. def sort_column
  80. columns = %w[name]
  81. columns[params[:iSortCol_0].to_i]
  82. end
  83. def sort_direction
  84. params[:sSortDir_0] == "desc" ? "desc" : "asc"
  85. end
  86. end