class DashboardController < ApplicationController
  def index
    @open_cash = false
    if current_user.usertype == 'A'
      # obtener ingresos por punto de venta
      @incomings = ActionController::Base.helpers.sanitize(CashRegistersMove.incomings_per_period('day'))
      # obtener ranking de productos y prepararlo para mandarlo al view
      @product_ranking = Array.new
      most_selled_products = Product.most_selled_products('week', current_user)
      most_selled_products.each do |product, quantity|
        obj = { product: product, quantity: quantity }
        @product_ranking << obj
      end
      @product_ranking = ActionController::Base.helpers.sanitize(@product_ranking.to_json)
    elsif current_user.usertype == 'G'
      # obtener ranking de vendedores y prepararlo para mandarlo al view
      @sellers_ranking = Array.new
      sellers = Seller.get_ranking('week', current_user.pointsale)
      sellers.each do |seller, total|
        obj = { seller: seller, total: total }
        @sellers_ranking << obj
      end
      @sellers_ranking = ActionController::Base.helpers.sanitize(@sellers_ranking.to_json)
      # obtener ranking de productos y prepararlo para mandarlo al view
      @product_ranking = Array.new
      most_selled_products = Product.most_selled_products('week', current_user)
      most_selled_products.each do |product, quantity|
        obj = { product: product, quantity: quantity }
        @product_ranking << obj
      end
      @product_ranking = ActionController::Base.helpers.sanitize(@product_ranking.to_json)
      session[:open_cash_register_id] = current_user.pointsale.get_open_cash_register.id if current_user.pointsale.get_open_cash_register.present?
    elsif current_user.usertype == 'C'
      @need_to_open_cash = current_user.pointsale.can_open_cash_register?
      session[:open_cash_register_id] = current_user.pointsale.get_open_cash_register.id unless @need_to_open_cash
    end
  end

  def get_chart_data_for_dashboard
    if params[:chart] == 'incomings'
      puts "incomings"
      incomings = CashRegistersMove.incomings_per_period(params[:period])
      @incomings = ActionController::Base.helpers.sanitize(incomings)
      render text: @incomings
    elsif params[:chart] == 'products'
      puts "products"
      @product_ranking = Array.new
      most_selled_products = Product.most_selled_products(params[:period], current_user)
      most_selled_products.each do |product, quantity|
        obj = { product: product, quantity: quantity }
        @product_ranking << obj
      end
      @product_ranking = ActionController::Base.helpers.sanitize(@product_ranking.to_json)
      render text: @product_ranking
    elsif params[:chart] == 'sellers'
      puts "sellers"
      @sellers_ranking = Array.new
      sellers = Seller.get_ranking(params[:period], current_user.pointsale)
      sellers.each do |seller, total|
        obj = { seller: seller, total: total }
        @sellers_ranking << obj
      end
      @sellers_ranking = ActionController::Base.helpers.sanitize(@sellers_ranking.to_json)
      render text: @sellers_ranking
    end
  end
end
