stock_by_pointsale_datatable.rb 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. subs_ids = Category.activos.where(parent_id: category).pluck(:id)
  73. stock = stock.joins(:categories).where('categories.id IN (?)', subs_ids)
  74. end
  75. if params[:length].to_i != -1
  76. stock = stock.page(page).per_page(per_page)
  77. end
  78. search = params[:busqueda]
  79. unless search.blank?
  80. stock = stock.where(prepare_query_string(search))
  81. end
  82. stock
  83. end
  84. def prepare_query_string(query)
  85. product_query =
  86. if query.include? ':' # search with attributes
  87. query_array = query.split(':')
  88. @product_name = query_array[0]
  89. query_array.shift # delete the name of the product from the array to iterate the attributes
  90. attrs_query_string = ''
  91. query_array.each do |attribute|
  92. next if attribute.nil?
  93. attr_type =
  94. case attribute[0]
  95. when 'c'
  96. 'colors'
  97. when 't'
  98. 'sizes'
  99. when 'e'
  100. 'styles'
  101. end
  102. attribute[0] = "" # delete the attribute type character
  103. attrs_query_string.concat(" AND products.attributes_json::json->>'#{attr_type}' ilike '%#{attribute}%'")
  104. end
  105. "products.sku ilike '%#{@product_name}%' or products.name ilike '%#{@product_name}%' #{attrs_query_string}"
  106. else
  107. "products.sku ilike '%#{query}%' or products.name ilike '%#{query}%'"
  108. end
  109. product_query
  110. end
  111. def page
  112. params[:start].to_i / per_page + 1
  113. end
  114. def per_page
  115. params[:length].to_i > 0 ? params[:length].to_i : 20
  116. end
  117. def sort_column
  118. columns = %w[name]
  119. columns[params[:iSortCol_0].to_i]
  120. end
  121. def sort_direction
  122. params[:sSortDir_0] == "desc" ? "desc" : "asc"
  123. end
  124. end