dashboard_controller.rb 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. class DashboardController < ApplicationController
  2. def index
  3. @open_cash = false
  4. if current_user.usertype == 'A'
  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. elsif current_user.usertype == 'C'
  33. opened_cash_user = OpenCashRegister.where(:user_id => current_user.id, :status => 0).any?
  34. @open_cash = true if opened_cash_user == false
  35. end
  36. end
  37. def get_chart_data_for_dashboard
  38. if params[:chart] == 'incomings'
  39. puts "incomings"
  40. incomings = CashRegistersMove.incomings_per_period(params[:period])
  41. @incomings = ActionController::Base.helpers.sanitize(incomings)
  42. render :text => @incomings
  43. elsif params[:chart] == 'products'
  44. puts "products"
  45. @product_ranking = Array.new
  46. most_selled_products = Product.most_selled_products(params[:period], current_user)
  47. most_selled_products.each do |product, quantity|
  48. obj = {:product => product, :quantity => quantity}
  49. @product_ranking << obj
  50. end
  51. @product_ranking = ActionController::Base.helpers.sanitize(@product_ranking.to_json)
  52. render :text => @product_ranking
  53. elsif params[:chart] == 'sellers'
  54. puts "sellers"
  55. @sellers_ranking = Array.new
  56. sellers = Seller.get_ranking(params[:period], current_user.pointsale)
  57. sellers.each do |seller, total|
  58. obj = {:seller => seller, :total => total}
  59. @sellers_ranking << obj
  60. end
  61. @sellers_ranking = ActionController::Base.helpers.sanitize(@sellers_ranking.to_json)
  62. render :text => @sellers_ranking
  63. end
  64. end
  65. end