stock_by_pointsale_datatable.rb 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. class StockByPointsaleDatatable
  2. delegate :params, :image_tag, :is_a, :number_to_currency, :available_products_path, :stock_by_pointsale_path, to: :@view
  3. def initialize(view, user, selected)
  4. @view = view
  5. @current_user = user
  6. @selected = selected
  7. end
  8. def as_json(_options = {})
  9. total = WarehouseStock.activos.where('stock > 0').count + AvailableProduct.activos.where('stock > 0').count
  10. {
  11. sEcho: params[:sEcho].to_i,
  12. iTotalRecords: total,
  13. iTotalDisplayRecords: (params[:length].to_i != -1 ? stock.total_entries : stock.size),
  14. aaData: data
  15. }
  16. end
  17. private
  18. def data
  19. stock.map do |available|
  20. img = available.product.img_product? ? available.product.img_product.url(:small) : "/images/small/missing.png"
  21. if @current_user.usertype == 'C'
  22. [
  23. (image_tag img),
  24. get_display_name(available.product),
  25. (available.product.category.parent_id.zero? ? available.product.category.category : available.product.category.parent.category),
  26. (available.product.category.parent_id != 0 ? available.product.category.category : ' '),
  27. available.stock.round,
  28. "<h3> #{get_price_sale(available.product)} </h3>"
  29. ].compact
  30. else
  31. arr = [
  32. (image_tag img),
  33. get_display_name(available.product),
  34. (available.product.category.parent_id.zero? ? available.product.category.category : available.product.category.parent.category),
  35. (available.product.category.parent_id != 0 ? available.product.category.category : ' '),
  36. available.stock_min.round,
  37. available.stock_max.round,
  38. available.stock.round
  39. ]
  40. arr << get_price_sale(available.product) unless @current_user.usertype == "S"
  41. arr.compact
  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 += "SKU: #{product.sku}"
  49. name += "<br> <i class='fa fa-barcode'></i>: #{product.barcode}" if product.barcode.present?
  50. name
  51. end
  52. def get_price_sale(product)
  53. location_id = params[:location][2, params[:location].length] unless params[:location].blank?
  54. "<h3> $#{product.get_price_sale(location_id)} </h3>"
  55. end
  56. def stock
  57. @stock ||= fetch_stock
  58. end
  59. def fetch_stock
  60. location = params[:location]
  61. category = params[:category]
  62. sub_category = params[:sub_category]
  63. location_id = params[:location][2, params[:location].length] unless location.blank?
  64. if (@current_user.usertype == 'S' && location.blank?) || (!location.blank? && location.first == 'W')
  65. stock = WarehouseStock.activos.includes(:categories, :warehouse).where(warehouse_id: (location_id.blank? ? @current_user.warehouse_id : location_id)).where('stock > 0')
  66. elsif (@current_user.usertype != 'S' && location.blank?) || (!location.blank? && location.first == 'P')
  67. stock = AvailableProduct.activos.includes(:categories, :pointsale).where(pointsale_id: (location_id.blank? ? @current_user.pointsale_id : location_id)).where('stock > 0')
  68. end
  69. if sub_category.present?
  70. stock = stock.joins(:categories).where('categories.id = ?', sub_category)
  71. elsif category.present?
  72. category = Category.find(category)
  73. children = category.children
  74. stock = stock.joins(:categories).where('categories.id IN (?)', children.present? ? children.pluck(:id) : category.id)
  75. end
  76. if params[:length].to_i != -1
  77. stock = stock.page(page).per_page(per_page)
  78. end
  79. search = params[:busqueda]
  80. unless search.blank?
  81. stock = stock.where(prepare_query_string(search))
  82. end
  83. stock
  84. end
  85. def prepare_query_string(query)
  86. product_query =
  87. if query.include? ':' # search with attributes
  88. query_array = query.split(':')
  89. @product_name = query_array[0]
  90. query_array.shift # delete the name of the product from the array to iterate the attributes
  91. attrs_query_string = ''
  92. query_array.each do |attribute|
  93. next if attribute.nil?
  94. attr_type =
  95. case attribute[0]
  96. when 'c'
  97. 'colors'
  98. when 't'
  99. 'sizes'
  100. when 'e'
  101. 'styles'
  102. end
  103. attribute[0] = "" # delete the attribute type character
  104. attrs_query_string.concat(" AND products.attributes_json::json->>'#{attr_type}' ilike '%#{attribute}%'")
  105. end
  106. "products.sku ilike '%#{@product_name}%' or products.name ilike '%#{@product_name}%' #{attrs_query_string}"
  107. else
  108. "products.sku ilike '%#{query}%' or products.name ilike '%#{query}%'"
  109. end
  110. product_query
  111. end
  112. def page
  113. params[:start].to_i / per_page + 1
  114. end
  115. def per_page
  116. params[:length].to_i > 0 ? params[:length].to_i : 20
  117. end
  118. def sort_column
  119. columns = %w[name]
  120. columns[params[:iSortCol_0].to_i]
  121. end
  122. def sort_direction
  123. params[:sSortDir_0] == "desc" ? "desc" : "asc"
  124. end
  125. end