Fat Free CRM Copyright (C) 2008-2011 by Michael Dvorkin

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see <www.gnu.org/licenses/>.

Methods
C
D
E
F
G
I
N
O
R
S
U
Instance Public methods
contacts()

GET /accounts/contacts AJAX

# File app/controllers/accounts_controller.rb, line 213
def contacts
  @account = Account.my.find(params[:id])
end
create()

POST /accounts POST /accounts.json POST /accounts.xml AJAX

# File app/controllers/accounts_controller.rb, line 101
def create
  @account = Account.new(params[:account])
  @users = User.except(@current_user)

  respond_to do |format|
    if @account.save_with_permissions(params[:users])
      # None: account can only be created from the Accounts index page, so we
      # don't have to check whether we're on the index page.
      @accounts = get_accounts
      get_data_for_sidebar
      format.js   # create.js.rjs
      format.json { render :json => @account, :status => :created, :location => @account }
      format.xml  { render :xml => @account, :status => :created, :location => @account }
    else
      format.js   # create.js.rjs
      format.json { render :json => @account.errors, :status => :unprocessable_entity }
      format.xml  { render :xml => @account.errors, :status => :unprocessable_entity }
    end
  end
end
destroy()

DELETE /accounts/1 DELETE /accounts/1.xml HTML and AJAX

# File app/controllers/accounts_controller.rb, line 150
def destroy
  @account = Account.my.find(params[:id])
  @account.destroy if @account

  respond_to do |format|
    format.html { respond_to_destroy(:html) }
    format.js   { respond_to_destroy(:ajax) }
    format.json { head :ok }
    format.xml  { head :ok }
  end

rescue ActiveRecord::RecordNotFound
  respond_to_not_found(:html, :js, :json, :xml)
end
edit()

GET /accounts/1/edit AJAX

# File app/controllers/accounts_controller.rb, line 85
def edit
  @account = Account.my.find(params[:id])
  @users = User.except(@current_user)
  if params[:previous].to_s =~ /(\d+)\z/
    @previous = Account.my.find($1)
  end

rescue ActiveRecord::RecordNotFound
  @previous ||= $1.to_i
  respond_to_not_found(:js) unless @account
end
filter()

POST /accounts/filter AJAX

# File app/controllers/accounts_controller.rb, line 225
def filter
  session[:filter_by_account_category] = params[:category]
  @accounts = get_accounts(:page => 1)
  render :index
end
index()

GET /accounts GET /accounts.json GET /accounts.xml HTML and AJAX

# File app/controllers/accounts_controller.rb, line 28
def index
  @accounts = get_accounts(:page => params[:page])

  respond_to do |format|
    format.html # index.html.haml
    format.js   # index.js.rjs
    format.json { render :json => @accounts }
    format.xml  { render :xml => @accounts }
    format.xls  { send_data @accounts.to_xls, :type => :xls }
    format.csv  { send_data @accounts.to_csv, :type => :csv }
    format.rss  { render "shared/index.rss.builder" }
    format.atom { render "shared/index.atom.builder" }
  end
end
new()

GET /accounts/new GET /accounts/new.json GET /accounts/new.xml AJAX

# File app/controllers/accounts_controller.rb, line 68
def new
  @account = Account.new(:user => @current_user, :access => Setting.default_access)
  @users = User.except(@current_user)
  if params[:related]
    model, id = params[:related].split("_")
    instance_variable_set("@#{model}", model.classify.constantize.find(id))
  end

  respond_to do |format|
    format.js   # new.js.rjs
    format.json { render :json => @account }
    format.xml  { render :xml => @account }
  end
end
opportunities()

GET /accounts/opportunities AJAX

# File app/controllers/accounts_controller.rb, line 219
def opportunities
  @account = Account.my.find(params[:id])
end
options()

GET /accounts/options AJAX

# File app/controllers/accounts_controller.rb, line 193
def options
  unless params[:cancel].true?
    @per_page = @current_user.pref[:accounts_per_page] || Account.per_page
    @outline  = @current_user.pref[:accounts_outline]  || Account.outline
    @sort_by  = @current_user.pref[:accounts_sort_by]  || Account.sort_by
  end
end
redraw()

POST /accounts/redraw AJAX

# File app/controllers/accounts_controller.rb, line 203
def redraw
  @current_user.pref[:accounts_per_page] = params[:per_page] if params[:per_page]
  @current_user.pref[:accounts_outline]  = params[:outline]  if params[:outline]
  @current_user.pref[:accounts_sort_by]  = Account::sort_by_map[params[:sort_by]] if params[:sort_by]
  @accounts = get_accounts(:page => 1)
  render :index
end

GET /accounts/search/query AJAX

# File app/controllers/accounts_controller.rb, line 167
def search
  @accounts = get_accounts(:query => params[:query], :page => 1)

  respond_to do |format|
    format.js   { render :index }
    format.json { render :json => @accounts.as_json }
    format.xml  { render :xml => @accounts.to_xml }
  end
end
show()

GET /accounts/1 GET /accounts/1.json GET /accounts/1.xml HTML

# File app/controllers/accounts_controller.rb, line 47
def show
  @account = Account.my.find(params[:id])
  @stage = Setting.unroll(:opportunity_stage)
  @comment = Comment.new

  @timeline = timeline(@account)

  respond_to do |format|
    format.html # show.html.haml
    format.json { render :json => @account }
    format.xml  { render :xml => @account }
  end

rescue ActiveRecord::RecordNotFound
  respond_to_not_found(:html, :json, :xml)
end
update()

PUT /accounts/1 PUT /accounts/1.json PUT /accounts/1.xml AJAX

# File app/controllers/accounts_controller.rb, line 126
def update
  @account = Account.my.find(params[:id])

  respond_to do |format|
    if @account.update_with_permissions(params[:account], params[:users])
      get_data_for_sidebar
      format.js
      format.json { head :ok }
      format.xml  { head :ok }
    else
      @users = User.except(@current_user) # Need it to redraw [Edit Account] form.
      format.js
      format.json { render :json => @account.errors, :status => :unprocessable_entity }
      format.xml  { render :xml => @account.errors, :status => :unprocessable_entity }
    end
  end

rescue ActiveRecord::RecordNotFound
  respond_to_not_found(:js, :json, :xml)
end
Instance Private methods
get_accounts(options = {})
# File app/controllers/accounts_controller.rb, line 233
def get_accounts(options = {})
  get_list_of_records(Account, options.merge!(:filter => :filter_by_account_category))
end
get_data_for_sidebar()
# File app/controllers/accounts_controller.rb, line 255
def get_data_for_sidebar
  @account_category_total = Hash[
    Setting.account_category.map do |key|
      [ key, Account.my.where(:category => key.to_s).count ]
    end
  ]
  categorized = @account_category_total.values.sum
  @account_category_total[:all] = Account.my.count
  @account_category_total[:other] = @account_category_total[:all] - categorized
end
respond_to_destroy(method)
# File app/controllers/accounts_controller.rb, line 238
def respond_to_destroy(method)
  if method == :ajax
    @accounts = get_accounts
    get_data_for_sidebar
    if @accounts.blank?
      @accounts = get_accounts(:page => current_page - 1) if current_page > 1
      render :index and return
    end
    # At this point render default destroy.js.rjs template.
  else # :html request
    self.current_page = 1 # Reset current page to 1 to make sure it stays valid.
    flash[:notice] = "#{t(:asset_deleted, @account.name)}"
    redirect_to accounts_path
  end
end