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

	#before_action :check_installation
	protect_from_forgery with: :exception

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

  def current_user
    @current_user ||= Employee.find_by_token_session(session[:session_token]) if session[:session_token]
  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]
	    from = params[:from]
	    to = params[:to]   
	    day_ref = Time.now.utc.getlocal

	    if params[:report_type] == "daily_sale" || params[:report_type] == "sale_item"

		    if from  != "" && to  != ""

		        f_date = DateTime.parse(params[:from])
				t_date = DateTime.parse(params[:to])
				f_time = Time.mktime(f_date.year,f_date.month,f_date.day,f_date.hour,f_date.min,f_date.sec)
				t_time = Time.mktime(t_date.year,t_date.month,t_date.day,t_date.hour,t_date.min,t_date.sec)
				from   = f_time.beginning_of_day.utc.getlocal
			    to     = t_time.end_of_day.utc.getlocal  

		    else 
		                
		        case period.to_i
		          when PERIOD["today"]

		            from = day_ref.beginning_of_day.utc
		            to = day_ref.end_of_day.utc

		          when PERIOD["yesterday"]
		            from = (day_ref - 1.day).beginning_of_day.utc
		            to = (day_ref - 1.day).end_of_day.utc

		          when PERIOD["this_week"]
		            from = Time.now.beginning_of_week.utc
		            to = Time.now.utc
		          when PERIOD["last_week"]
		            from = (day_ref - 7.day).beginning_of_week.utc
		            to = (day_ref - 7.day).end_of_week.utc
		          when PERIOD["last_7"]
		            from = (day_ref - 7.day).utc
		            to = Time.now.utc
		          when PERIOD["this_month"]
		            from = Time.now.beginning_of_month.utc
		            to = Time.now.utc
		          when PERIOD["last_month"]
		            from = (day_ref - 1.month).beginning_of_month.utc
		            to = (day_ref - 1.month).end_of_month.utc
		          when PERIOD["last_30"]
		            from = (day_ref - 30.day).utc
		            to = Time.now.utc
		          when PERIOD["this_year"]
		            from = Time.now.beginning_of_year.utc
		            to = Time.now.utc
		          when PERIOD["last_year"]
		            from = (day_ref - 1.year).beginning_of_year.utc
		            to = (day_ref - 1.year).end_of_year.utc
		        end  
		    end
	    else # end daily sale report
	    	if period_type.to_i == 1
	    	
		            if params[:from] != "" && params[:to] !=""

		    		  f_date = DateTime.parse(params[:from])
				      t_date = DateTime.parse(params[:to])
				      f_time = Time.mktime(f_date.year,f_date.month,f_date.day,f_date.hour,f_date.min,f_date.sec)
				      t_time = Time.mktime(t_date.year,t_date.month,t_date.day,t_date.hour,t_date.min,t_date.sec)
				      from   = f_time.beginning_of_day.utc.getlocal
				      to     = t_time.end_of_day.utc.getlocal
		         	else
			            from = day_ref.beginning_of_day.utc
			            to   = day_ref.end_of_day.utc
			        end  
		    else 	            
		        case period.to_i
		          when PERIOD["today"]

		            from = day_ref.beginning_of_day.utc
		            to = day_ref.end_of_day.utc

		          when PERIOD["yesterday"]
		            from = (day_ref - 1.day).beginning_of_day.utc
		            to = (day_ref - 1.day).end_of_day.utc

		          when PERIOD["this_week"]
		            from = Time.now.beginning_of_week.utc
		            to = Time.now.utc
		          when PERIOD["last_week"]
		            from = (day_ref - 7.day).beginning_of_week.utc
		            to = (day_ref - 7.day).end_of_week.utc
		          when PERIOD["last_7"]
		            from = (day_ref - 7.day).utc
		            to = Time.now.utc
		          when PERIOD["this_month"]
		            from = Time.now.beginning_of_month.utc
		            to = Time.now.utc
		          when PERIOD["last_month"]
		            from = (day_ref - 1.month).beginning_of_month.utc
		            to = (day_ref - 1.month).end_of_month.utc
		          when PERIOD["last_30"]
		            from = (day_ref - 30.day).utc
		            to = Time.now.utc
		          when PERIOD["this_year"]
		            from = Time.now.beginning_of_year.utc
		            to = Time.now.utc
		          when PERIOD["last_year"]
		            from = (day_ref - 1.year).beginning_of_year.utc
		            to = (day_ref - 1.year).end_of_year.utc
		        end  
		    end
	    end

	    return from, to 


	end
  
end
