class BaseReportController < ActionController::Base
  include MultiTenancy
  include LoginVerification
	layout "application"

	before_action :check_user

	#before_action :check_installation
	protect_from_forgery with: :exception

	rescue_from CanCan::AccessDenied do |exception|
	    flash[:warning] = exception.message
	    redirect_to root_path
  	end

	PERIOD = {
		"today" => 0,
		"yesterday" => 1,
		"this_week" => 2,
		"last_week" => 3,
		"last_7" => 4,
		"this_month" => 5,
		"last_month" => 6,
		"last_30" => 7,
		"this_year" => 8,
		"last_year" => 9
	}

	def get_date_range_from_params
    period_type = params[:period_type]
    period = params[:period]

    if params[:from].present? && params[:to].present?
      from = Time.zone.parse(params[:from])
      to = Time.zone.parse(params[:to])
    else
      case period.to_i
        when PERIOD["today"]
          from = Time.current
          to = Time.current
        when PERIOD["yesterday"]
          from = 1.day.ago
          to = 1.day.ago
        when PERIOD["this_week"]
          from = Time.current.beginning_of_week
          to = Time.current
        when PERIOD["last_week"]
          from = 1.week.ago.beginning_of_week
          to = 1.week.ago.end_of_week
        when PERIOD["last_7"]
          from = 7.day.ago
          to = Time.current
        when PERIOD["this_month"]
          from = Time.current.beginning_of_month
          to = Time.current
        when PERIOD["last_month"]
          from = 1.month.ago.beginning_of_month
          to = 1.month.ago.end_of_month
        when PERIOD["last_30"]
          from = 30.day.ago
          to = Time.current
        when PERIOD["this_year"]
          from = Time.current.beginning_of_year
          to = Time.current
        when PERIOD["last_year"]
          from = 1.year.ago.beginning_of_year
          to = 1.year.ago.end_of_year
      end
    end

    from = from.beginning_of_day
    to = to.end_of_day

    return from, to
	end

	def check_user
		if current_user.nil?
		  redirect_to root_path
		end
	end
end
