jeudi 2 juillet 2015

Rails Viewing Multiple Stats within Admin Namespace

I have an Admin Namespace where all my controllers live. What I am want to be able to view is create stats on the view layer on all of these view. IE have the latest sales value on the top righthand side no matter which page you are on.

I have a few point that i have achieve

1) the data need to be in a the period 26th to the 25 of the next month. this must be on the current month. So today it would pull 26/6/2015 - 25/07/2015 this would only change again on the 26/7 to the new month. Any ideas code is below.

2) The code i have below is pulling the data it is very rough at this point for testing, also i need it to pull rep information so require rep_id which is giving a Null error as the users is not logged in yet. Once logged in it works apart from part 1.

Application Controller

  protect_from_forgery
  helper_method :current_user
  before_filter :loadlatestquotes
  before_filter :loadstats

  rescue_from CanCan::AccessDenied do |exception|
    redirect_to admin_url
  end

  private

  def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end

  def loadlatestquotes
    @latestquotes = Quote.where(:rep_id => current_user.rep_id, :created_at => (8640.hours.ago..Time.now))
  end

  def loadstats
    @latestaccepedquotes = Quote.where(:rep_id => current_user.rep_id, :quote_accepted => true, :created_at => (8640.hours.ago..Time.now)).count
    @latestrejectedquotes = Quote.where(:rep_id => current_user.rep_id, :rejected => true, :created_at => (8640.hours.ago..Time.now)).count
    @latestpendingquotes = Quote.where(:rep_id => current_user.rep_id, :quote_accepted => false, :rejected => false, :created_at => (8640.hours.ago..Time.now)).count
    @latesttotalquote = Quote.where(:rep_id => current_user.rep_id, :created_at => (8640.hours.ago..Time.now)).sum(:quote_amount)
  end

  def logged_in?
    unless session[:user_id]
      flash[:notice] = "You need to log in first."
      redirect_to login_path
      return false
    else
      return true
    end
  end 

In my view i have the following

Total Quote Value: <%= number_to_currency(@latesttotalquote, :unit => "R") %><br /> 
Total Order Value: <br />
Total Pending Quotes:  <%= @latestpendingquotes %><br />
Total Accepted Quotes: <%= @latestaccepedquotes %> <br />
Total Rejected Quotes: <%= @latestrejectedquotes %> <br />
Total Quotes:  <%= @latestquotes.count %><br /> 
Lead Closure:  <br />
Rejection Rate:  <br />
Rand Closure:  <br />

Thanks for the assistance in advanced. Any assistance would be great in order for me to fix this thanks

Aucun commentaire:

Enregistrer un commentaire