git-svn-id: https://192.168.0.254/svn/Rodax.redmine_rodax_crm/trunk@2 ff88604e-da85-c949-a72f-fc3aa3ba3724
248 lines
8.1 KiB
Ruby
248 lines
8.1 KiB
Ruby
class DealsController < ApplicationController
|
|
unloadable
|
|
|
|
PRICE_TYPE_PULLDOWN = [l(:label_price_fixed_bid), l(:label_price_per_hour)]
|
|
|
|
before_filter :find_deal, :only => [:show, :edit, :update, :destroy]
|
|
before_filter :find_project_by_project_id, :only => [:new, :create]
|
|
before_filter :bulk_find_deals, :only => [:bulk_update, :bulk_edit, :bulk_destroy, :context_menu]
|
|
before_filter :authorize, :except => [:index]
|
|
before_filter :find_optional_project, :only => [:index]
|
|
# before_filter :find_deals, :only => :index
|
|
before_filter :update_deal_from_params, :only => [:edit, :update]
|
|
before_filter :build_new_deal_from_params, :only => [:new, :create]
|
|
before_filter :find_deal_attachments, :only => :show
|
|
|
|
helper :attachments
|
|
helper :contacts
|
|
helper :notes
|
|
helper :timelog
|
|
helper :watchers
|
|
helper :custom_fields
|
|
include WatchersHelper
|
|
include DealsHelper
|
|
|
|
def new
|
|
@deal = Deal.new
|
|
@deal.contact = Contact.find(params[:contact_id]) if params[:contact_id]
|
|
@deal.assigned_to = User.current
|
|
end
|
|
|
|
def create
|
|
@deal = Deal.new(params[:deal])
|
|
# @deal.contacts = [Contact.find(params[:contacts])]
|
|
@deal.project = @project
|
|
@deal.author = User.current
|
|
@deal.init_deal_process(User.current)
|
|
if @deal.save
|
|
flash[:notice] = l(:notice_successful_create)
|
|
redirect_to :action => "show", :id => @deal
|
|
else
|
|
render :action => "new"
|
|
end
|
|
end
|
|
|
|
def update
|
|
@deal.init_deal_process(User.current)
|
|
if @deal.update_attributes(params[:deal])
|
|
# @deal.contacts = [Contact.find(params[:contacts])] if params[:contacts]
|
|
flash[:notice] = l(:notice_successful_update)
|
|
respond_to do |format|
|
|
format.html { redirect_to :action => "show", :id => @deal }
|
|
format.xml { }
|
|
end
|
|
else
|
|
respond_to do |format|
|
|
format.html { render :action => "edit"}
|
|
end
|
|
end
|
|
|
|
end
|
|
|
|
def edit
|
|
respond_to do |format|
|
|
format.html { }
|
|
format.xml { }
|
|
end
|
|
end
|
|
|
|
def index
|
|
retrieve_deals_query
|
|
params[:status_id] = "o" unless params.has_key?(:status_id)
|
|
find_deals
|
|
respond_to do |format|
|
|
format.html{ request.xhr? ? render( :partial => "list", :layout => false, :locals => {:deals => @deals}) : last_notes }
|
|
format.xml { render :xml => @deals}
|
|
format.json { render :text => @deals.to_json, :layout => false }
|
|
end
|
|
end
|
|
|
|
def show
|
|
@note = DealNote.new
|
|
respond_to do |format|
|
|
format.html { @deal.viewed }
|
|
format.xml { }
|
|
end
|
|
end
|
|
|
|
def destroy
|
|
if @deal.destroy
|
|
flash[:notice] = l(:notice_successful_delete)
|
|
else
|
|
flash[:error] = l(:notice_unsuccessful_save)
|
|
end
|
|
redirect_to :action => "index", :project_id => params[:project_id]
|
|
|
|
end
|
|
|
|
def context_menu
|
|
@deal = @deals.first if (@deals.size == 1)
|
|
@can = {:edit => User.current.allowed_to?(:edit_deals, @projects),
|
|
:delete => User.current.allowed_to?(:delete_deals, @projects)
|
|
}
|
|
|
|
# @back = back_url
|
|
render :layout => false
|
|
end
|
|
|
|
def bulk_destroy
|
|
@deals.each do |deal|
|
|
begin
|
|
deal.reload.destroy
|
|
rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists
|
|
# nothing to do, issue was already deleted (eg. by a parent)
|
|
end
|
|
end
|
|
respond_to do |format|
|
|
format.html { redirect_back_or_default(:action => 'index', :project_id => @project) }
|
|
format.api { head :ok }
|
|
end
|
|
end
|
|
|
|
def bulk_edit
|
|
@available_statuses = @projects.map(&:deal_statuses).inject{|memo,w| memo & w}
|
|
@available_categories = @projects.map(&:deal_categories).inject{|memo,w| memo & w}
|
|
@assignables = @projects.map(&:assignable_users).inject{|memo,a| memo & a}
|
|
end
|
|
|
|
|
|
def bulk_update
|
|
unsaved_deal_ids = []
|
|
@deals.each do |deal|
|
|
deal.reload
|
|
deal.init_deal_process(User.current)
|
|
unless deal.update_attributes(parse_params_for_bulk_deal_attributes(params))
|
|
# Keep unsaved issue ids to display them in flash error
|
|
unsaved_deal_ids << deal.id
|
|
end
|
|
if params[:note] && !params[:note][:content].blank?
|
|
note = DealNote.new(params[:note])
|
|
note.author = User.current
|
|
deal.notes << note
|
|
end
|
|
|
|
end
|
|
set_flash_from_bulk_issue_save(@deals, unsaved_deal_ids)
|
|
redirect_back_or_default({:controller => 'deals', :action => 'index', :project_id => @project})
|
|
|
|
end
|
|
|
|
private
|
|
|
|
def last_notes(count=5)
|
|
# TODO: Исправить говнокод этот и выделить все в плагин acts-as-noteble
|
|
scope = DealNote.scoped({})
|
|
scope = scope.scoped(:conditions => ["#{Deal.table_name}.project_id = ?", @project.id]) if @project
|
|
|
|
@last_notes = scope.visible.find(:all,
|
|
:limit => count,
|
|
:order => "#{DealNote.table_name}.created_on DESC")
|
|
end
|
|
|
|
|
|
def build_new_deal_from_params
|
|
end
|
|
|
|
def update_deal_from_params
|
|
end
|
|
|
|
def find_deal_attachments
|
|
@deal_attachments = Attachment.find(:all,
|
|
:conditions => { :container_type => "Note", :container_id => @deal.notes.map(&:id)},
|
|
:order => "created_on DESC")
|
|
end
|
|
|
|
|
|
def find_deals(pages=true)
|
|
retrieve_date_range(params[:period].to_s)
|
|
|
|
scope = Deal.scoped({})
|
|
scope = scope.scoped(:conditions => ["#{Deal.table_name}.project_id = ?", @project.id]) if @project
|
|
scope = scope.scoped(:conditions => ["#{Deal.table_name}.status_id = ?", params[:status_id]]) if (!params[:status_id].blank? && params[:status_id] != "o")
|
|
scope = scope.scoped(:conditions => ["#{Deal.table_name}.category_id = ?", params[:category_id]]) if !params[:category_id].blank?
|
|
scope = scope.scoped(:conditions => ["#{Deal.table_name}.assigned_to_id = ?", params[:assigned_to_id]]) if !params[:assigned_to_id].blank?
|
|
scope = scope.scoped(:conditions => ["#{Deal.table_name}.created_on BETWEEN ? AND ?", @from, @to]) if (@from && @to)
|
|
|
|
params[:search].split(' ').collect{ |search_string| scope = scope.live_search(search_string) } if !params[:search].blank?
|
|
scope = scope.visible
|
|
scope = scope.scoped(:include => :status, :conditions => ["#{DealStatus.table_name}.is_closed = ?", false]) if (params[:status_id] == "o")
|
|
scope = scope.scoped(:order => :status_id)
|
|
|
|
|
|
@deals_count = scope.count
|
|
|
|
if pages
|
|
@deals_sum = scope.sum(:price, :group => :currency)
|
|
|
|
page_size = params[:page_size].blank? ? 20 : params[:page_size].to_i
|
|
@deals_pages = Paginator.new(self, @deals_count, page_size, params[:page])
|
|
@offset = @deals_pages.current.offset
|
|
@limit = @deals_pages.items_per_page
|
|
|
|
scope = scope.scoped :limit => @limit, :offset => @offset
|
|
@deals = scope
|
|
|
|
fake_name = @deals.first.price if @deals.length > 0 #without this patch paging does not work
|
|
end
|
|
|
|
scope
|
|
|
|
end
|
|
|
|
# Filter for bulk issue operations
|
|
def bulk_find_deals
|
|
@deals = Deal.find_all_by_id(params[:id] || params[:ids], :include => :project)
|
|
raise ActiveRecord::RecordNotFound if @deals.empty?
|
|
if @deals.detect {|deal| !deal.visible?}
|
|
deny_access
|
|
return
|
|
end
|
|
@projects = @deals.collect(&:project).compact.uniq
|
|
@project = @projects.first if @projects.size == 1
|
|
rescue ActiveRecord::RecordNotFound
|
|
render_404
|
|
end
|
|
|
|
|
|
def find_deal
|
|
@deal = Deal.find(params[:id], :include => [:project, :status, :category])
|
|
@project ||= @deal.project
|
|
# if !(params[:project_id] == @project.identifier)
|
|
# params[:project_id] = @project.identifier
|
|
# redirect_to params
|
|
# end
|
|
rescue ActiveRecord::RecordNotFound
|
|
render_404
|
|
end
|
|
|
|
def parse_params_for_bulk_deal_attributes(params)
|
|
attributes = (params[:deal] || {}).reject {|k,v| v.blank?}
|
|
attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
|
|
attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values]
|
|
attributes
|
|
end
|
|
|
|
|
|
|
|
end
|