stock_by_pointsale_datatable.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. class StockByPointsaleDatatable
  2. delegate :params, :image_tag, :is_a, :number_to_currency,
  3. :available_products_path, :stock_by_pointsale_path,
  4. to: :@view
  5. def initialize(view, user, selected)
  6. @view = view
  7. @current_user = user
  8. @selected = selected
  9. end
  10. def as_json(options = {})
  11. total = WarehouseStock.activos.where('stock > 0').count + AvailableProduct.activos.where('stock > 0').count
  12. {
  13. sEcho: params[:sEcho].to_i,
  14. iTotalRecords: total,
  15. iTotalDisplayRecords: (params[:length].to_i != -1 ? stock.total_entries : stock.size),
  16. aaData: data
  17. }
  18. end
  19. private
  20. def data
  21. stock.map do |available|
  22. if available.product.img_product?
  23. img = available.product.img_product.url(:small)
  24. else
  25. img = "/images/small/missing.png"
  26. end
  27. category = available.categories[0]
  28. if @current_user.usertype == 'C'
  29. [
  30. (image_tag img),
  31. get_display_name(available.product),
  32. (category.parent_id == 0 ? category.category : category.parent.category),
  33. (category.parent_id != 0 ? category.category : ' '),
  34. # available.stock.round,
  35. "<h3> $#{get_price_sale(available.product)} </h3>"
  36. ].compact
  37. else
  38. [
  39. (image_tag img),
  40. get_display_name(available.product),
  41. (category.parent_id == 0 ? category.category : category.parent.category),
  42. (category.parent_id != 0 ? category.category : ' '),
  43. available.stock_min.round,
  44. available.stock_max.round,
  45. available.stock.round,
  46. "<h3> $#{get_price_sale(available.product)} </h3>"
  47. ].compact
  48. end
  49. end
  50. end
  51. def get_display_name(product)
  52. name = "<label style='margin-bottom:0px'><strong>#{product.name}</strong></label> <br>"
  53. if product.display_attributes.present?
  54. name += "#{product.display_attributes} <br>"
  55. end
  56. name += "SKU: #{product.sku}"
  57. if product.barcode.present?
  58. name += "<br> <i class='fa fa-barcode'></i>: #{product.barcode}"
  59. end
  60. return name
  61. end
  62. def get_price_sale(product)
  63. if @current_user.usertype != 'S'
  64. location_id = params[:location][2, params[:location].length] if !params[:location].blank?
  65. return product.get_price_sale(location_id)
  66. else
  67. return ''
  68. end
  69. end
  70. def stock
  71. @stock ||= fetch_stock
  72. end
  73. def fetch_stock
  74. location = params[:location]
  75. category = params[:category]
  76. sub_category = params[:sub_category]
  77. location_id = params[:location][2, params[:location].length] if !location.blank?
  78. if ( @current_user.usertype == 'S' && location.blank? ) || ( !location.blank? && location.first == 'W' )
  79. stock = WarehouseStock.activos.includes(:categories, :warehouse).where(:warehouse_id => (location_id.blank? ? @current_user.warehouse_id : location_id) ).where('stock > 0')
  80. elsif ( @current_user.usertype != 'S' && location.blank? ) || ( !location.blank? && location.first == 'P' )
  81. stock = AvailableProduct.activos.includes(:categories, :pointsale).where(:pointsale_id => (location_id.blank? ? @current_user.pointsale_id : location_id) ).where('stock > 0')
  82. end
  83. if sub_category.present?
  84. stock = stock.joins(:categories).where('categories.id = ?', sub_category)
  85. elsif category.present?
  86. subs_ids = Category.activos.where(:parent_id => category).pluck(:id)
  87. stock = stock.joins(:categories).where('categories.id IN (?)', subs_ids)
  88. end
  89. if params[:length].to_i != -1
  90. stock = stock.page(page).per_page(per_page)
  91. end
  92. unless params[:busqueda].blank?
  93. stock = stock.where("products.sku ilike :search or products.name ilike :search", search: "%#{params[:busqueda]}%")
  94. end
  95. stock
  96. end
  97. def page
  98. params[:start].to_i/per_page + 1
  99. end
  100. def per_page
  101. params[:length].to_i > 0 ? params[:length].to_i : 20
  102. end
  103. def sort_column
  104. columns = %w[name]
  105. columns[params[:iSortCol_0].to_i]
  106. end
  107. def sort_direction
  108. params[:sSortDir_0] == "desc" ? "desc" : "asc"
  109. end
  110. end