dashboard_controller.rb 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. class DashboardController < ApplicationController
  2. def index
  3. @open_cash = false
  4. if current_user.usertype == "A" || current_user.usertype == "SS"
  5. # obtener ingresos por punto de venta
  6. @incomings = ActionController::Base.helpers.sanitize(CashRegistersMove.incomings_per_period('day'))
  7. # obtener ranking de productos y prepararlo para mandarlo al view
  8. @product_ranking = Array.new
  9. most_selled_products = Product.most_selled_products('week', current_user)
  10. most_selled_products.each do |product, quantity|
  11. obj = { product: product, quantity: quantity }
  12. @product_ranking << obj
  13. end
  14. @product_ranking = ActionController::Base.helpers.sanitize(@product_ranking.to_json)
  15. elsif current_user.usertype == 'G'
  16. # obtener ranking de vendedores y prepararlo para mandarlo al view
  17. @sellers_ranking = Array.new
  18. sellers = Seller.get_ranking('week', current_user.pointsale)
  19. sellers.each do |seller, total|
  20. obj = { seller: seller, total: total }
  21. @sellers_ranking << obj
  22. end
  23. @sellers_ranking = ActionController::Base.helpers.sanitize(@sellers_ranking.to_json)
  24. # obtener ranking de productos y prepararlo para mandarlo al view
  25. @product_ranking = Array.new
  26. most_selled_products = Product.most_selled_products('week', current_user)
  27. most_selled_products.each do |product, quantity|
  28. obj = { product: product, quantity: quantity }
  29. @product_ranking << obj
  30. end
  31. @product_ranking = ActionController::Base.helpers.sanitize(@product_ranking.to_json)
  32. session[:open_cash_register_id] = current_user.pointsale.get_open_cash_register.id if current_user.pointsale.get_open_cash_register.present?
  33. elsif current_user.usertype == 'C'
  34. @need_to_open_cash = current_user.pointsale.can_open_cash_register?
  35. session[:open_cash_register_id] = current_user.pointsale.get_open_cash_register.id unless @need_to_open_cash
  36. end
  37. end
  38. def get_chart_data_for_dashboard
  39. if params[:chart] == 'incomings'
  40. puts "incomings"
  41. incomings = CashRegistersMove.incomings_per_period(params[:period])
  42. @incomings = ActionController::Base.helpers.sanitize(incomings)
  43. render text: @incomings
  44. elsif params[:chart] == 'products'
  45. puts "products"
  46. @product_ranking = Array.new
  47. most_selled_products = Product.most_selled_products(params[:period], current_user)
  48. most_selled_products.each do |product, quantity|
  49. obj = { product: product, quantity: quantity }
  50. @product_ranking << obj
  51. end
  52. @product_ranking = ActionController::Base.helpers.sanitize(@product_ranking.to_json)
  53. render text: @product_ranking
  54. elsif params[:chart] == 'sellers'
  55. puts "sellers"
  56. @sellers_ranking = Array.new
  57. sellers = Seller.get_ranking(params[:period], current_user.pointsale)
  58. sellers.each do |seller, total|
  59. obj = { seller: seller, total: total }
  60. @sellers_ranking << obj
  61. end
  62. @sellers_ranking = ActionController::Base.helpers.sanitize(@sellers_ranking.to_json)
  63. render text: @sellers_ranking
  64. end
  65. end
  66. end