stocks_datatable.rb 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. class StocksDatatable
  2. delegate :params, :image_tag,
  3. :available_products_path, :products_initial_stock_path,
  4. to: :@view
  5. def initialize(view, user, showcolumns)
  6. @view = view
  7. @current_user = user
  8. @showcolumns = showcolumns
  9. end
  10. def as_json(options = {})
  11. if @current_user.usertype == 'S'
  12. total = WarehouseStock.activos.where(:warehouse_id => @current_user.pointsale_id)
  13. else
  14. total = AvailableProduct.activos.where(:pointsale_id => @current_user.warehouse_id)
  15. end
  16. {
  17. sEcho: params[:sEcho].to_i,
  18. iTotalRecords: total.size,
  19. iTotalDisplayRecords: stock.total_entries,
  20. aaData: data
  21. }
  22. end
  23. private
  24. def data
  25. stock.map do | available |
  26. if available.product.img_product?
  27. img = available.product.img_product.url(:small)
  28. else
  29. img = "/images/small/missing.png"
  30. end
  31. category = available.product.categories[0]
  32. {
  33. 'DT_RowId' => "availableProduct_#{available.id}",
  34. '0' => '<input class="form-control checkboxes" type="checkbox"/>',
  35. '1' => (image_tag img),
  36. '2' => available.product.sku,
  37. '3' => "#{available.product.name + '<br>' + available.product.display_attributes}",
  38. '4' => (category.parent_id == 0 ? category.category : category.parent.category),
  39. '5' => (category.parent_id != 0 ? category.category : ' '),
  40. '6' => (@showcolumns == "minMax" ? available.stock_min : available.stock),
  41. '7' => (@showcolumns == "minMax" ? available.stock_max : nil)
  42. }.compact.reject { |k, v| v.nil? }
  43. end
  44. end
  45. def stock
  46. @stock ||= fetch_stock
  47. end
  48. def fetch_stock
  49. if @current_user.usertype == 'S'
  50. stock = WarehouseStock.activos.includes(:product, :categories).where(:warehouse_id => @current_user.warehouse_id).order('products.name')
  51. else
  52. stock = AvailableProduct.activos.includes(:product, :categories).where(:pointsale_id => @current_user.pointsale_id).order('products.name')
  53. end
  54. stock = stock.page(page).per_page(per_page)
  55. unless params[:busqueda].blank?
  56. stock = stock.where("products.sku ilike :search or products.name ilike :search", search: "%#{params[:busqueda]}%").order('products.name')
  57. end
  58. stock
  59. end
  60. def page
  61. params[:start].to_i/per_page + 1
  62. end
  63. def per_page
  64. params[:length].to_i > 0 ? params[:length].to_i : 20
  65. end
  66. def sort_column
  67. columns = %w[name]
  68. columns[params[:iSortCol_0].to_i]
  69. end
  70. def sort_direction
  71. params[:sSortDir_0] == "desc" ? "desc" : "asc"
  72. end
  73. end